简体   繁体   English

使用 splice 将对象添加到对象数组

[英]Adding an object to an array of objects with splice

I have an array of objects that looks like this:我有一个看起来像这样的对象数组:

event_id=[{"0":"e1"},{"0","e2"},{"0","e4"}];

How do I add an element to that array?如何向该数组添加元素?

I thought of我想到了

event_id.splice(1,0,{"0":"e5"});

Thanks.谢谢。

If you just want to add a value to the end of an array then the push(newObj) function is easiest, although splice(...) will also work (just a bit trickier).如果您只想在数组的末尾添加一个值,那么push(newObj)函数是最简单的,尽管splice(...)也可以工作(只是有点棘手)。

var event_id = [{"0":"e1"}, {"0":"e2"}, {"0":"e4"}];
event_id.push({"0":"e5"});
//event_id.splice(event_id.length, 0, {"0":"e5"}); // Same as above.
//event_id[event_id.length] = {"0":"e5"}; // Also the same.
event_id; // => [{"0":"e1"}, {"0":"e2"}, {"0":"e4"}, {"0":"e5"}]; 

See the excellent MDN documentation for the Array object for a good reference of the methods and properties available on arrays.请参阅有关Array对象的优秀MDN 文档,以获得有关数组上可用的方法和属性的良好参考。

[Edit] To insert something into the middle of the array then you'll definitely want to use the splice(index, numToDelete, el1, el2, ..., eln) method which handles both deleting and inserting arbitrary elements at any position: [编辑]要在数组的中间插入一些东西,那么你肯定要使用splice(index, numToDelete, el1, el2, ..., eln)方法来处理在任何位置删除和插入任意元素:

var a  = ['a', 'b', 'e'];
a.splice( 2,   // At index 2 (where the 'e' is),
          0,   // delete zero elements,
         'c',  // and insert the element 'c',
         'd'); // and the element 'd'.
a; // => ['a', 'b', 'c', 'd', 'e']

Since I want to add the object in the middle of the array, I ended with this solution:由于我想在数组中间添加对象,我以这个解决方案结束:

var add_object = {"0": "e5"};
event_id.splice(n, 0, add_object); // n is declared and is the index where to add the object

ES6 solution with spread operator:带有扩展运算符的 ES6 解决方案:

event_id=[{"0":"e1"},{"0","e2"},{"0","e4"}];
event_id = [...event_id,{"0":"e5"}]

or if you do not want to mutate event_id或者如果您不想改变 event_id

newEventId = [...event_id,{"0":"e5"}]

UPDATE : to insert object after a specific index or object key or object value respectively you can:更新:分别在特定索引或对象键或对象值后插入对象,您可以:

const arr = [{a:1},{b:2},{c:3},{d:4}]

arr.reduce((list,obj,index)=>index===1 ? [...list,obj,{g:10}] : [...list,obj], [])
arr.reduce((list,obj)=>Object.keys(obj)[0]==='b' ? [...list,obj,{g:10}] : [...list,obj], [])
arr.reduce((list,obj)=>Object.values(obj)[0]===2 ? [...list,obj,{g:10}] : [...list,obj], [])

// output:  [ { a: 1 }, { b: 2 }, { g: 10 }, { c: 3 }, { d: 4 } ]
event_id.push({"something", "else"});

尝试使用.push(...) ^

Well you could typically use:那么你通常可以使用:

event_id[event_id.length] = {"0":"e5"};

or (the slightly slower)或(稍慢)

event_id.push({"0":"e5"});

though if you mean to insert an element into the middle of an array and not always on the end, then we'll have to come up with something a bit more creative.但是,如果您想将一个元素插入到数组的中间而不总是在最后,那么我们将不得不想出一些更有创意的东西。

Hope it helps,希望能帮助到你,

ise伊势

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

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