简体   繁体   English

如何在另一个位置的对象数组中添加对象?

[英]How to add objects within another array of object at alternate position?

I am working on a react component.我正在研究一个反应组件。 Requirement is to add a new object at alternate position within array of object eg below:要求是在对象数组中的备用位置添加一个新对象,例如:

arr1 = [{test: 1},{test: 2},{test: 3},{test: 4}]

expected output:预期输出:

arr1 = [{test: 1},{dummy:1},{test: 2},{dummy:1},{test: 3},{dummy:1},{test: 4}]

is there a way to do the same in es6?有没有办法在es6中做同样的事情?

For getting your desired array, you could make of use the concat method in combination with reduce .为了获得所需的数组,您可以将concat方法与reduce结合使用。

 var array = [{test: 1},{test: 2},{test: 3},{test: 4}]; var result = array.reduce((res, item) => res.concat(item, {dummy: 1}), []).slice(0, -1); console.log(result);

Since this adds {dummy: 1} object after each element from your array, just use由于这会在数组中的每个元素之后添加{dummy: 1}对象,因此只需使用

.slice(0, -1)

to add one exception for the last element from your given array.给定数组中的最后一个元素添加一个例外

If you want to modify the original array inplace , you could use splice method.如果要就地修改原始数组,可以使用splice方法。

 var array = [{test: 1},{test: 2},{test: 3},{test: 4}]; for(i = 0;i < array.length - 1; i = i + 2){ array.splice(i + 1, 0, {dummy: 1}); } console.log(array);

I like to use map if I can.如果可以,我喜欢使用地图。 It is easier to understand and maintain.更容易理解和维护。

So first create an array of array and then flatten it a single array with flat.因此,首先创建一个数组数组,然后将其扁平化为单个数组。

 let arr1 = [{test: 1},{test: 2},{test: 3},{test: 4}] arr1 = arr1.map(item=>[item,{dummy:1}]).flat().slice(0, -1) console.log(arr1);

To modify the original array (instead of creating an new one), you can first generate the indexes at which the new items need to be inserted, and then use Array.prototype.splice() :要修改原始数组(而不是创建新数组),您可以先生成需要插入新项的索引,然后使用Array.prototype.splice()

 const a = [{test: 1}, {test: 2}, {test: 3}, {test: 4}]; Array.from({length: a.length - 1}, (_, i) => i * 2 + 1) .forEach(i => a.splice(i, 0, {dummy: 1})); console.log(a);

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

相关问题 在另一个数组中的对象内的一个对象数组中添加对象内容 - Add object contents in one object array within objects in another array 如何将对象添加到Angular中另一个动态创建的对象中的键中? - How to add objects to a key within another dynamically created object in Angular? 如何将对象构造函数添加到另一个对象内的空数组中 - How to add an object constructor into an empty array that is within another object 如何将 object 添加到另一个对象数组中的数组? - How to add object to an array inside another array of objects? 如何将对象数组中的 object 推送到另一个 object 数组中的数组? - How can I push an object in an array of objects to an array within another array of object? 如何在基于另一个 object 的嵌套对象数组中添加键 - How to add key in a nested array of Objects based on another object 如何用另一个对象数组更改一个对象数组中的对象值? - How to change object value within an array of objects with one from another array of objects? 如何将 for 循环中一个数组的值添加到另一个数组并将其推送到 object - How to add the values of one array within a for loop to another and push it to an object 从另一个对象数组向一个对象添加值 - Add values to an object from another array of objects 将复杂的对象数组添加到另一个 object - Add complex array of objects into another object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM