Reputation: 2096
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.
Upvotes: 2
Views: 4640
Reputation: 191976
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);
Upvotes: 3
Reputation: 950
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);
},[]
)
Upvotes: 0
Reputation: 39270
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]
])
);
Upvotes: 0
Reputation: 697
How about this simple reduce?
const orderedSeries = series.reduce((orderedAcum, elem) => [
...orderedAcum, ...(Array.isArray(elem) ? elem : [elem])
], []);
Upvotes: 1
Reputation: 58
You can do it this way
let newArr = [];
series.forEach((item) => {
newArr = newArr.concat(item);
});
series = newArr;
Upvotes: 0
Reputation: 7250
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);
Upvotes: 0
Reputation: 370769
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);
Upvotes: 0