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.

Harsh
  • 1,956
  • 6
  • 20
  • 37
  • 3
    Possible duplicate of [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – Vikram Palakurthi Apr 04 '18 at 01: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. – HMR Apr 04 '18 at 04:05

7 Answers7

2

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);
Ori Drori
  • 145,770
  • 24
  • 170
  • 162
1

How about this simple reduce?

const orderedSeries = series.reduce((orderedAcum, elem) => [
    ...orderedAcum, ...(Array.isArray(elem) ? elem : [elem])
], []);
fethe
  • 631
  • 6
  • 12
  • 1
    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). – HMR Apr 04 '18 at 03:55
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);
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
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);
    },[]
)
SKLTFZ
  • 730
  • 1
  • 8
  • 22
  • 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). – HMR Apr 04 '18 at 04:00
  • yes, adding a initial value then concat can be applied – SKLTFZ Apr 04 '18 at 06:17
0

You can do it this way

let newArr = [];
series.forEach((item) => {
  newArr = newArr.concat(item);
});
series = newArr;
Smite107
  • 58
  • 1
  • 6
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);
Jorg
  • 6,881
  • 3
  • 38
  • 61
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]
  ])
);
HMR
  • 30,349
  • 16
  • 67
  • 136