简体   繁体   中英

How to push a set of arrays into main array?

I want to push some arrays into the main array in single function. Here is the example:

let MainArr = [
 {
  "date": "2021-02-24T18:30:00.000Z",
  "age": "9",
  "id": 74,
  "time": 4.00 },
 {"date": "2021-02-24T18:30:00.000Z",
  "age":"9",
  "id": 74,
  "time": 4.00
}];

I am getting all this data from API so next array will be like this:

let arr1 = [{
  "id": 1,
  "arrKey1": "schedule1"
  }, {
  "id": 2,
  "arrKey1": "schedule2"
  }]
let arr2 = [{
  "id": 1,
  "arrKey2": "bus1"
}, {
  "id": 2,
  "arrKey2": "bus2"
}]
let arr3 = [{
  "id": 1,
  "arrKey3": "car1"
}, {
  "id": 2,
  "arrKey3": "1"
}]

Now the result should reflect like this:

  [{
    "date": "2021-02-24T18:30:00.000Z",
    "age": "9",
    "id": 74,
    "time": 4.00,
    "arrKey1":"schedule1",
    "arrKey2 ":"bus1",
    "arrKey3":"car1"},
   {"date ": "2021-02-24T18:30:00.000Z",
    "age": "9",
    "id": 74,
    "time": 4.00,
    "arrKey1": "schedule2",
    "arrKey2": "bus2",
    "arrKey3": "1"
  }];

In ES6 you can use three dots. see the example:

 let MainArr = [1, 2, 3]; let arr1 = [4, 5, 6]; console.log('before: ', MainArr) MainArr.push(...arr1) console.log('after pushing arr1: ', MainArr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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