2

I have an array that has values at two indexes and i want to populate the values of two indexes into one index probably in a different array.

this.nodesObjResultSingular =  (2) [Array(6), Array(6)]

this.nodesObjResultSingular = [
[
    {sets: Array(1), size: 12},
    {sets: Array(1), size: 12},
    {sets: Array(1), size: 12},
    {sets: Array(2), size: 2},
    {sets: Array(2), size: 2},
    {sets: Array(2), size: 2}
],
[{sets: Array(1), size: 12},
{sets: Array(1), size: 12},
{sets: Array(1), size: 12},
{sets: Array(2), size: 2},
{sets: Array(2), size: 2},
{sets: Array(2), size: 2}]
];

How can I get the following ?

finalVenn = // all the 12 values of this.nodesObjResultSingular.
4
  • Want to combines the two array? Commented Dec 13, 2018 at 12:30
  • two is just an example... populate more than two arrays into one single array Commented Dec 13, 2018 at 12:37
  • 1
    const flattened = [].concat(...nodesObjResultSingular ) Commented Dec 13, 2018 at 12:49
  • 1
    or..... developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 13, 2018 at 12:50

2 Answers 2

3

Use spread operator to concat them. like:

finalVenn = [...this.nodesObjResultSingular[0], ...this.nodesObjResultSingular[1]]

Or could also use Array.concat()

finalVenn = this.nodesObjResultSingular[0].concat(this.nodesObjResultSingular[1])

If you have multiple indeces to concat then use:

var multipleArr = [[1], [2, 3], [4, 5, 6 ]]
var res = [].concat.apply([], multipleArr)
console.log(res)

Sign up to request clarification or add additional context in comments.

Comments

1

If you have an unknown number if arrays, you can use reduce with concat:

let data = [
[{sets: Array(1), size: 12},{sets: Array(1), size: 12},{sets: Array(1), size: 12},{sets: Array(2), size: 2},{sets: Array(2), size: 2},{sets: Array(2), size: 2}],
[{sets: Array(1), size: 12},{sets: Array(1), size: 12},{sets: Array(1),size: 12},{sets: Array(2), size: 2},{sets: Array(2), size: 2},{sets: Array(2), size: 2}]];

var merged = data.reduce((acc, arr) => acc.concat(arr), []);
console.log(merged);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.