How to manipulate array of objects in Javascript?

Abstract

In Javascript, we often need to manipulate array objects. This post mainly introduces: ① How to get the index of array elements? ② How to delete array elements? ③ How to slice array elements? ④ How to insert elements in the array? ⑤ How to concat arraies?

Keywords: Javascript , array , indexOf , delete , slice , insert.

IndexOf

Sometimes we need to get the index of an array element. In this case, we can use the indexOf() method. If no element is found, the function returns -1. It should be noted that if the array or object is stored in the array, the returned result of indexOf() will always be -1. It is necessary to compare the sub-objects in the elements one by one to see if they are equal to obtain the index. Or you can use JSON.stringify() methond to compare two object, but if the order of object key are need to be consistent, otherwise, you will always get False even compare two same object.

				
					var test_array=['1','2','3'];
var index_1=test_array.indexOf('3'); //index_1=2
var index_2=test_array.indexOf('4'); //index_1=-1


//if object in array
var arr_list=[{"id":1},{"id":2}];
var object_index=arr_list.indexOf({"id":1}); // object_index=-1
var object_index_2=getIndexFromObjectArray(arr_list,{"id":1}); //object_index_2=0

function getIndexFromObjectArray(arr_list,e){
    for(var i=0;i<arr_list.length;i++){
        if(JSON.stringify(arr_list[i]) == JSON.stringify(e)){
            return i;
        }
    }
    return -1;
}

				
			

Delete

When deleting elements from an array, you can use the splice(start_index, count) function, where start_index indicates the position where the start index, and count indicates how many elements to delete from start_index.

				
					var arr_list=[{"id":1},{"id":2},{"id":3}];
var left_arr=arr_list.splice(0,2); // left_arr=[{"id":1}]

				
			

Slice

Use slice(start_index, end_index) function when intercepting elements of an array.

				
					var arr_list=[1,2,3,4];
var slice_arr=arr_list.slice(0,2); // slice_arr=[1,2,3]

				
			

Insert

To insert element in an array, you can use the unshift() or push() methods. The unshift() method inserts element at the beginning of array, and the push() method inserts element at the end.

				
					var arr_list=[1,2,3];
var arr_list_2=arr_list.unshift(4); //arr_list_2=[4,1,2,3]
var arr_list_3=arr_list_2.push(5) //arr_list_3=[4,1,2,3,5]

				
			

Concat

If you want to concat two arraies, there are concat(), apply(), push() and for..loop methods.

				
					var a = [1,2,3];
var b = [4,5,6];

// concat() will keep a,b value and create clone data to c
var c = a.concat(b);//c=[1,2,3,4,5,6]

// for...loop
for(var i in b){
    a.push(b[i]);
}

//apply() method
a.push.apply(a,b);
				
			

Summarize

In Javascript, we can use indexOf() to get element index,use splice() to remove element,use unshift() and push() to insert element, use concat(), apply() and for…loop to concatenate arrays.

Related articles

Coming soon

If yuo have any question or need any help 

you can leave a comment or connect me with weichaoxu1998@gmail.com

Edit Template

Leave a Reply

Your email address will not be published. Required fields are marked *

Chaoxu Wei

Master student at Tongji University (Writer & Blogger)

Job hunting, both part-time and full-time jobs are accepted. Job recommendation please email me weichaoxu1998@gmail.com, thank you very much!

Contents

0
0
Edit Template

“There are many people in the world who want to work hard but can’t, and people who work too hard and are exhausted. Please don’t use your efforts only to chase your own victory, please use them to help others.”

Follow

Contact

libertynlp@163.com

+86-18385537403

Yangpu District, Shanghai