简体   繁体   中英

How to insert/add an object to object array?

I thought the below is logically sound in adding or inserting object into an object array but the result I am getting is funky. Can someone please tell me what I am doing wrong here?? Why is below code returning empty array when I am trying to insert an object into an object array in the index of 0 and why is below code returning 4 when I am trying to add an object at the end of the object array?

 let objArr = [{ id: 1, company: "Rapid Precision Mfg.", title: "Quality Engineer", firstName: "Dongyob", lastName: "Lee", officePh: "", ext: "", cell: "669-294-0910", email: "dyl4810@gmail.com" }, { id: 2, company: "Facebook", title: "Frontend Developer", firstName: "Edward", lastName: "Simmons", officePh: "408-516-4662", ext: "003", cell: "669-252-4251", email: "edwardsimmons@gmail.com" } ] let nobj = { id: 1, company: "Rapid Precision Mfg.", title: "Quality Engineer", firstName: "Dongyob", lastName: "Lee", officePh: "", ext: "", cell: "669-294-0910", email: "dyl4810@gmail.com" } console.log(objArr.splice(0, 0, nobj)) //Outcome: [] console.log(objArr.push(nobj)) //Outcome: 4 

splice returns the removed elements of the array. If you don't remove any elements in the splice call, the returned array will be empty.

 const arr = [0, 1, 2, 3]; // from index 0, don't remove any elements, and insert element 'foo': console.log(arr.splice(0, 0, 'foo')); 

push returns the new length of the array. The output is 4 because the array started with 2 items, you splice d one item in (making the length 3), then push ed another item in, making the length 4.

Your current code

console.log(objArr.splice(0, 0, nobj))
console.log(objArr.push(nobj))

is inserting nobj into the last position and into the first position in the array - if you want to see what the array is afterwards, log the array:

console.log(objArr);

Note that rather than splice ing in an element at index 0, you can use unshift instead:

 const arr = [0, 1, 2, 3]; arr.unshift('foo'); console.log(arr); 

The splice syntax is array.splice(index, howmany, item1, ....., itemX) and it returns the removed one.

If you want to test your change do console.log(objArr)

 let objArr = [{ id: 1, company: "Rapid Precision Mfg.", title: "Quality Engineer", firstName: "Dongyob", lastName: "Lee", officePh: "", ext: "", cell: "669-294-0910", email: "dyl4810@gmail.com" }, { id: 2, company: "Facebook", title: "Frontend Developer", firstName: "Edward", lastName: "Simmons", officePh: "408-516-4662", ext: "003", cell: "669-252-4251", email: "edwardsimmons@gmail.com" } ] let nobj = { id: 1, company: "Rapid Precision Mfg.", title: "Quality Engineer", firstName: "Dongyob", lastName: "Lee", officePh: "", ext: "", cell: "669-294-0910", email: "dyl4810@gmail.com" } objArr.splice(0, 0, nobj) console.log(objArr) //Outcome: [] //console.log(objArr.push(nobj)) 

As the above users have mentioned, splice() returns the removed element(s) of the array. If you are trying to push an object to the start of your array, another way to do so would be to use the spread syntax (yay ES6!!). I think it is much more readable than the method you are trying to employ.

//[nobj, ...objArr]
console.log([nobj, ...objArr]);

Unlike unshift(), it does not modify the original objArr array in-place.

const newArray = [nobj, ...objArr];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM