Home > Back-end >  push the contents of array into another array without looping
push the contents of array into another array without looping

Time:10-22

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.

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:

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?

CodePudding user response:

You can spread the content of both arrays into the new array

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.

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);

CodePudding user response:

Use 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) );
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  1. Merge using array.concat() method

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Mutable merge of arrays

Merge it into existing array.

  1. Merge using array.push() method

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);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Using underscore _.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>
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Using 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);
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Using 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);
<iframe name="sif7" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use concat() to merge the arrays and create a single new array:

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>
<iframe name="sif8" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Another option using 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)
<iframe name="sif9" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

i think this will work for you.

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

CodePudding user response:

The spread operator mentioned above is the best 2021 way to do it.

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