简体   繁体   English

如何将对象插入/添加到对象数组?

[英]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? 当我尝试将对象插入索引为0的对象数组中时,为什么下面的代码返回空数组;当我尝试在对象数组的末尾添加对象时,为什么下面的代码返回4?

 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. splice返回数组中已删除的元素 If you don't remove any elements in the splice call, the returned array will be empty. 如果您不删除splice调用中的任何元素,则返回的数组将为空。

 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. push返回数组的新长度 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. 输出为4,因为数组以2个项目开始,您将一个项目splice (长度为3),然后push另一个项目push ,长度为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: 正在将nobj插入到数组的最后一个位置第一个位置-如果要查看之后的数组,请记录该数组:

console.log(objArr);

Note that rather than splice ing in an element at index 0, you can use unshift instead: 请注意,您可以使用unshift代替在索引0处splice元素:

 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. 拼接语法为array.splice(index, howmany, item1, ....., itemX) ,它返回removed

If you want to test your change do console.log(objArr) 如果要测试更改,请执行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. 如上述用户所述, splice()返回数组中已删除的元素。 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!!). 如果您试图将一个对象推到数组的开头,那么另一种方法是使用传播语法 (是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. 与unshift()不同,它不会objArr修改原始objArr数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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