ES2019
ES2019 introduced the Array.prototype.flat() method which you could use to flatten the arrays. It is compatible with most environments, although it is only available in Node.js starting with version 11, and not at all in Internet Explorer.
const arrays = [
["
12"],
["
25"],
["
22"],
["$10"]
];
const merge3 = arrays.flat(1); //The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
console.log(merge3);
Older browsers
For older browsers, you can use Array.prototype.concat to merge arrays:
var arrays = [
["
12"],
["
25"],
["
22"],
["$10"]
];
var merged = [].concat.apply([], arrays);
console.log(merged);
Using the apply method of concat will just take the second parameter as an array, so the last line is identical to this:
var merged = [].concat(["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]);
Answer from Gumbo on Stack OverflowES2019
ES2019 introduced the Array.prototype.flat() method which you could use to flatten the arrays. It is compatible with most environments, although it is only available in Node.js starting with version 11, and not at all in Internet Explorer.
const arrays = [
["
12"],
["
25"],
["
22"],
["$10"]
];
const merge3 = arrays.flat(1); //The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
console.log(merge3);
Older browsers
For older browsers, you can use Array.prototype.concat to merge arrays:
var arrays = [
["
12"],
["
25"],
["
22"],
["$10"]
];
var merged = [].concat.apply([], arrays);
console.log(merged);
Using the apply method of concat will just take the second parameter as an array, so the last line is identical to this:
var merged = [].concat(["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]);
Here's a short function that uses some of the newer JavaScript array methods to flatten an n-dimensional array.
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
Usage:
flatten([[1, 2, 3], [4, 5]]); // [1, 2, 3, 4, 5]
flatten([[[1, [1.1]], 2, 3], [4, 5]]); // [1, 1.1, 2, 3, 4, 5]
You can use reduce() and spread syntax.
var arr = [
[ {"c": 1},{"d": 2} ],
[ {"c": 2},{"d": 3} ]
]
var result = arr.reduce((r, e) => (r.push(...e), r), [])
console.log(result)
You could use Array#reduce with Array#concat.
var arr = [[{ c: 1 }, { d: 2 }], [{ c: 2 }, { d: 3 }]],
result = arr.reduce((r, a) => r.concat(a), []);
console.log(result)
ES5
var arr = [[{ c: 1 }, { d: 2 }], [{ c: 2 }, { d: 3 }]],
result = arr.reduce(function (r, a) { return r.concat(a); }, []);
console.log(result)
You can use ES6's spread:
var arrays = [[1, 2], [3, 4], [5, 6]];
var res = [].concat(...arrays);
console.log(res);
Use reduce and concat
var output = arr.reduce( (a, c) => a.concat(c), []); //assuming arr is the input array
Edit
As @TJ mentioned in his comment, that above solution will create some intermediate arrays along the way, you can try (concat without spread)
var output = [].concat.apply([], arr);
or
var output = Array.prototype.concat.apply([], arr); //avoiding usage of another unnecessary array `[]`
Update: This is 2019 and Array.flat is native.
const val = {
_id: ["5412fc1bd123cf7016674a92", "5412cf270e9ca9b517b43ca3"],
_id2: ["5412cf5a6cc4f4bc151fd220"]
}
console.log(
Object.values(val).flat()
)
// Without flat
console.log(
Array.prototype.concat.apply(
[],
Object.values(val)
)
)
// Without Object.values
console.log(
Array.prototype.concat.apply(
[],
Object.keys(val).map(k => val[k])
)
)
The following is all you need with lodash:
_.flatten(_.values(val))
Assuming your input data is an object containing several arrays, like so:
var val = {
_id: ["5412fc1bd123cf7016674a92", "5412cf270e9ca9b517b43ca3"],
_id2: ["5412cf5a6cc4f4bc151fd220"]
};
You can get the desired array structure pretty easily using concat:
var flat = [];
for (var key in val) {
flat = flat.concat(val[key]);
}
console.log(flat);
Output:
[ '5412fc1bd123cf7016674a92',
'5412cf270e9ca9b517b43ca3',
'5412cf5a6cc4f4bc151fd220' ]
Use Array#map function and get also the index, which you will use to get the items from the second and third array. I used also || if first array has more items that the others.
let x = ['a', 'b', 'c',' d'];
let y = ['e', 'f', 'g', 'h'];
let w = ['i', 'j', 'k', 'l'];
let mapped = x.map((item, index) => ({ x: item, y: y[index] || '', w: w[index] || '' }));
console.log(mapped);
Try
var z = x.map( (s,i) => ({ x : x[i], y : y[i], w : w[i] }) );
Explanation
- iterate x using
map - for each index of
x, return an object having keys asx,yandzwith values from their respective index.
Demo
var x = ['a', 'b', 'c', 'd'];
var y = ['e', 'f', 'g', 'h'];
var w = ['i', 'j', 'k', 'l'];
var z = x.map((s, i) => ({
x: x[i],
y: y[i],
w: w[i]
}));
console.log(z);
arr.flatMap(Object.values).flat() will do the job:
const arr = [{JUNE: [{"iD": "67854", "event": " closed", "title": "test", "startDate": "2024-06-20", "endDate": "2024-07-25" } ] }, {MAY: [{"ID": "908765", "event": "closed", "title": "test", "startDate": "2024-05-21", "endDate": "2024-06-27" }, {ID: "12345", event: "open", title: "test123", startDate: "2024-05-21", endDate: "2024-06-27" } ] } ];
const result = arr.flatMap(Object.values).flat();
console.log(result)
If you don't need the months, but just want to flatten it, flatMap and flat() it :
const arr = [{JUNE: [{"iD": "67854", "event": " closed", "title": "test", "startDate": "2024-06-20", "endDate": "2024-07-25" } ] }, {MAY: [{"ID": "908765", "event": "closed", "title": "test", "startDate": "2024-05-21", "endDate": "2024-06-27" }, {ID: "12345", event: "open", title: "test123", startDate: "2024-05-21", endDate: "2024-06-27" } ] } ];
const res = arr.flatMap(c => Object.values(c).flat());
console.log(res)
If you want to add the month, thenreduce the array and flatMap each Object.keys with an deeper map for all the events.
const arr = [{JUNE: [{"iD": "67854", "event": " closed", "title": "test", "startDate": "2024-06-20", "endDate": "2024-07-25" } ] }, {MAY: [{"ID": "908765", "event": "closed", "title": "test", "startDate": "2024-05-21", "endDate": "2024-06-27" }, {ID: "12345", event: "open", title: "test123", startDate: "2024-05-21", endDate: "2024-06-27" } ] } ];
const res = arr.reduce((p, c) => ([ ...p,
...(Object.keys(c)
.flatMap(month => c[month]
.map(o => ({ ...o, month })) ))
])
, []);
console.log(res)
Or as an one-liner":
const res = arr.reduce((p, c) => ([ ...p,...(Object.keys(c).flatMap(month => c[month].map(o => ({ ...o, month }))))]), []);
One quick way is to use Object.assign and spread operator
let arr = [{"1":"test1"},{"2":"test2"},{"3":"test3"}]
let obj = Object.assign({},...arr);
console.log(obj);
Doc: Object.assign, spread operator
var arr = [{"1":"test1"},{"2":"test2"},{"3":"test3"}];
var newarr = arr.reduce(function(data,val){
Object.assign(data[0],val);
return data;
},[{}]);
console.log(newarr);