I have a JavaScript Object and I’m sure the value of any key is an array (even empty in some case):
JavaScript
x
2
1
{key1:["a","b","c"],key2:["d","e","f"],key3: }
2
Aside from using Underscore, is there any way to concatenate all the values of this Object (and create a new array)?
At the moment I get the keys name using Object.keys
, then I loop and concatenate.
Any help is appreciated.
Advertisement
Answer
JavaScript
1
8
1
var obj = {key1:["a","b","c"],key2:["d","e","f"]};
2
3
var arr = Object.keys(obj).reduce(function(res, v) {
4
return res.concat(obj[v]);
5
}, []);
6
7
// ["a", "b", "c", "d", "e", "f"]
8