2
var series = 
[
    [{
        Name: "A",
        Type: "SC"
    }, {
        Name: "B",
        Type: "SC"
    }, {
        Name: "C",
        Type: "SC"
    }, {
        Name: "D",
        Type: "SC"
    }], 
    {
        ColumnName: "Target",
        Type: "Line"
    },
    {
        ColumnName: "bar",
        Type: "Line"
    }
];

I want to merge array as below

var series = [ { Name: "A", Type: "SC" }, { Name: "B", Type: "SC" }, { Name: "C", Type: "SC" }, { Name: "D", Type: "SC" }, { ColumnName: "Target", Type: "Line" }, { ColumnName: "bar", Type: "Line" } ];

Any help is highly appreciated.

2
  • 3
    Possible duplicate of How to merge two arrays in JavaScript and de-duplicate items Commented Apr 4, 2018 at 1:09
  • The correct jargon would be flatten an array, you are not merging 2 arrays because you don't have 2 arrays. You have an optionally 2 dimensional array that you want to flatten to a one dimensional array. Commented Apr 4, 2018 at 4:05

7 Answers 7

3

JS has the Array.flat() method that can flatten arrays:

const series = [[{"Name":"A","Type":"SC"},{"Name":"B","Type":"SC"},{"Name":"C","Type":"SC"},{"Name":"D","Type":"SC"}],{"ColumnName":"Target","Type":"Line"},{"ColumnName":"bar","Type":"Line"}];

const flattened = series.flat();

console.log(flattened);

Old answer:

Spread the array into Array.concat():

const series = [[{"Name":"A","Type":"SC"},{"Name":"B","Type":"SC"},{"Name":"C","Type":"SC"},{"Name":"D","Type":"SC"}],{"ColumnName":"Target","Type":"Line"},{"ColumnName":"bar","Type":"Line"}];

const flattened = [].concat(...series);

console.log(flattened);

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

1 Comment

@BoundForGlory - You're welcome, but see updated answer.
1

How about this simple reduce?

const orderedSeries = series.reduce((orderedAcum, elem) => [
    ...orderedAcum, ...(Array.isArray(elem) ? elem : [elem])
], []);

1 Comment

The reducer function could simply be (all,item)=>all.concat(item) to flatten a (possibly) 2 dimensional array. If the array is x dimensional you can recursively reduce (see my answer).
0

const input = [
  [{
    Name: "A",
    Type: "SC"
  }, {
    Name: "B",
    Type: "SC"
  }, {
    Name: "C",
    Type: "SC"
  }, {
    Name: "D",
    Type: "SC"
  }],
  {
    ColumnName: "Target",
    Type: "Line"
  },
  {
    ColumnName: "bar",
    Type: "Line"
  }
];

const output = input[0];
input.forEach((item, i) => {
  if (i >= 1) output.push(item);
});
console.log(output);

1 Comment

That basically is series[0].concat(series.slice(1))
0

Here's a one liner that alters the original array instead of creating a new one.

console.clear();

const input = [
  [{
    Name: "A",
    Type: "SC"
  }, {
    Name: "B",
    Type: "SC"
  }, {
    Name: "C",
    Type: "SC"
  }, {
    Name: "D",
    Type: "SC"
  }],
  {
    ColumnName: "Target",
    Type: "Line"
  },
  {
    ColumnName: "bar",
    Type: "Line"
  }
];

input.forEach((item, i) => (item instanceof Array) ? input.push(...item) && input.splice(i, 1) : false)

console.log(input);

Comments

0

You can do it this way

let newArr = [];
series.forEach((item) => {
  newArr = newArr.concat(item);
});
series = newArr;

Comments

0

Just concatenate the first element of the array with all but the first element:

var series = 
[
    [{
        Name: "A",
        Type: "SC"
    }, {
        Name: "B",
        Type: "SC"
    }, {
        Name: "C",
        Type: "SC"
    }, {
        Name: "D",
        Type: "SC"
    }], 
    {
        ColumnName: "Target",
        Type: "Line"
    },
    {
        ColumnName: "bar",
        Type: "Line"
    }
];

console.log(series[0].concat(series.slice(1)));
//to flatten a possibly 2 dimensional array to a one dimensional array:
console.log("flattened:");
console.log(
  series.reduce(
    (all,item)=>all.concat(item),
    []
  )
);

If you want to flatten an x dimensional to a one dimensional array you can use the following:

var flatten = arr => {
  const recur = arr =>
    arr.reduce(
      (all,item)=>
        (!Array.isArray(item))
          ? all.concat(item)
          : all.concat(recur(item)),
        []
    );
  return recur(arr);
}

console.log(
  flatten([
    [1,2,3],
    [
      [
        [4,5],
        [6],[[7]]
      ]
    ],
    8,
    [9,10]
  ])
);

Comments

0

you can try to use .reduce

var series = 
[
    [{
        Name: "A",
        Type: "SC"
    }, {
        Name: "B",
        Type: "SC"
    }, {
        Name: "C",
        Type: "SC"
    }, {
        Name: "D",
        Type: "SC"
    }], 
    {
        ColumnName: "Target",
        Type: "Line"
    },
    {
        ColumnName: "bar",
        Type: "Line"
    }
];
series.reduce(
    (x,y)=>{
        var merged = [];
        if(Array.isArray(x)) 
        {
            for(var p=0;p<x.length;p++){
                merged.push(x[p]);
            }
        }
        else merged.push(x);
        if(Array.isArray(y)) 
        {
            for(var p=0;p<y.length;p++){
                merged.push(y[p]);
            }
        }
        else merged.push(y);
        return merged;
    }
)

adding a initial value in the reduce it is possible to force an array concat

series.reduce(
    (x,y)=>{
      return x.concat(y);
    },[]
)

2 Comments

Using concat would be a lot simpler and and doesn't need to go over the accumulated array for every element. series.reduce((all,item)=>all.concat(item),[]) if you want to flatten a x dimensional array you can use reduce and recursion (see my answer).
yes, adding a initial value then concat can be applied

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.