Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139833

Javascript how to merge two child arrays as a single array

$
0
0

I want to make an array flat.

// example of the start array
const myArray = [
  [ 
    { foo: "bar", baz: "qux"},
    { quux: "corge", grault: "garply" }
  ],
  [
    { waldo: "fred", plugh: "xyzzy" },
    { thud: "barbra", streisand: "woohoo" }
  ]
];

However I'd like it to be:

// end result
[
  { foo: "bar", baz: "qux"},
  { quux: "corge", grault: "garply" },
  { waldo: "fred", plugh: "xyzzy" },
  { thud: "barbra", streisand: "woohoo" }
]

Now the following example gives the result: (2) [Array(2), Array(2)].

const myArray = [
  [ 
  	{ foo: "bar", baz: "qux"},
    { quux: "corge", grault: "garply" }
  ],
  [
  	{ waldo: "fred", plugh: "xyzzy" },
    { thud: "barbra", streisand: "woohoo" }
  ]
];

let newArray = [];
myArray.forEach((subArray) => newArray.push(subArray));
console.log(newArray);

Viewing all articles
Browse latest Browse all 139833

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>