简体   繁体   English

将数组的内容推入另一个数组而不循环

[英]push the contents of array into another array without looping

Here's a simple piece of JavaScript where I want to add the contents of orders.foo and orders2.foo to a single-dimensional ordersArr .这是一段简单的 JavaScript,我想将orders.fooorders2.foo的内容添加到一维ordersArr

let _ = require('underscore');

let ordersArr = [];
let orders = {
    foo: [
        {x: 1, b: 2},
        {y: 1, c: 3},
        {a: 2, d: 4}
    ]
}

ordersArr = _.map(orders.foo, order => order)

orders2 = {
        foo: [
                {x: 2, b: 3},
                {y: 5, c: 4},
                {a: 3, d: 6}
        ]
}


let tOrders = _.map(orders2.foo, order => order);
ordersArr.push(tOrders)

console.log(ordersArr);

The problem with this code is that push in this case creates a multi-dimensional array:这段代码的问题是在这种情况下push创建了一个多维数组:

Output输出

[
  { x: 1, b: 2 },
  { y: 1, c: 3 },
  { a: 2, d: 4 },
  [ { x: 2, b: 3 }, { y: 5, c: 4 }, { a: 3, d: 6 } ]
]

How do I iterate the contents of orders.foo and orders2.foo and have their results as one single dimension array?如何迭代orders.fooorders2.foo的内容并将它们的结果作为一维数组?

You can spread the content of both arrays into the new array您可以spread两个数组的内容spread到新数组中

const arr1 = [1,2,3];
const arr2 = [4,5,6];
const arr3 = [...arr1, ...arr2];
console.log(arr3);
// prints [1,2,3,4,5,6]

Spreading arr2 into arr1 also works.arr2传播到arr1也有效。

arr1.push(...arr2);
console.log(arr1);
// prints [1,2,3,4,5,6]

So changing如此变化

ordersArr.push(tOrders)

to

ordersArr.push(...tOrders);

should work.应该管用。

For a full answer:完整答案:

let ordersArr = [];
let orders = {
    foo: [
        {x: 1, b: 2},
        {y: 1, c: 3},
        {a: 2, d: 4}
    ]
}

orders2 = {
        foo: [
                {x: 2, b: 3},
                {y: 5, c: 4},
                {a: 3, d: 6}
        ]
}

ordersArr.push(...orders.foo, ...orders2.foo);

Using underscore _.flatten :使用下划线_.flatten

 const orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] }, orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] }; const ordersArr = _.flatten([orders.foo, orders2.foo]); console.log(ordersArr);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>

Using javascript spread operator :使用 javascript spread operator

 const orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] }, orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] }; const ordersArr = [...orders.foo, ...orders2.foo]; console.log(ordersArr);

Using javascript Array#concat :使用 javascript Array#concat

 const orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] }, orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] }; const ordersArr = orders.foo.concat(orders2.foo); console.log(ordersArr);

Use Array.concat()使用Array.concat()

 let orders1 = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] }; let orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] }; console.log( orders1.foo.concat(orders2.foo) );

You can use concat() to merge the arrays and create a single new array:您可以使用concat()合并数组并创建一个新数组:

let tOrders = orders.foo.concat(orders2.foo);

 let ordersArr = []; let orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] } ordersArr = _.map(orders.foo, order => order) orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] } let tOrders = orders.foo.concat(orders2.foo); console.log(tOrders)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>

Another option using flat()使用flat() 的另一种选择

 let ordersArr = []; let orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] } orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] } let tOrders = [orders.foo, orders2.foo].flat(); console.log(tOrders)

Immutable merge of arrays数组的不可变合并

Creates a new array.创建一个新数组。

  1. Merge using the spread operator使用扩展运算符合并

 let orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] } let orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] } const mergeResult = [...orders.foo, ...orders2.foo]; console.log(mergeResult);

  1. Merge using array.concat() method使用array.concat()方法合并

 let orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] } let orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] } const mergeResult = orders.foo.concat(orders2.foo); console.log(mergeResult);

Mutable merge of arrays数组的可变合并

Merge it into existing array.将其合并到现有数组中。

  1. Merge using array.push() method使用array.push()方法合并

 let orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] } let orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] } orders.foo.push(...orders2.foo); console.log(orders.foo);

i think this will work for you.我认为这对你有用。

let tOrders = _.map(orders2.foo, order => order);
tOrders.foreach((element)=>{
ordersArr.push(element)
})
console.log(ordersArr);

上面提到的价差运算符是 2021 年最好的方法。

let ordersArr = [...orders.foo, ...orders2.foo];

I'll add one more flavor to the list.我将在列表中添加另一种口味。 You can create a shallow copy of an array using the built-in slice method, which has been with us for a very long time:你可以使用内置的slice方法创建一个数组的浅拷贝,这个方法已经存在很长时间了:

var ordersArr = orders.foo.slice();

Now you can add the contents of the other array using push and apply :现在您可以使用pushapply添加另一个数组的内容:

ordersArr.push.apply(ordersArr, orders2.foo);

Et voilá, ordersArr is now a one-dimensional array containing all elements of both orders.foo and orders2.foo .等等, ordersArr现在是一个一维数组,包含orders.fooorders2.foo所有元素。 Works even in ES3!即使在 ES3 中也能工作!

For inspiration, you can find lots of nice little tricks like this in the Underscore source code .为了获得灵感,您可以在Underscore 源代码中找到许多这样的小技巧。

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

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