Skip to content
Advertisement

Concatenate Object values

I have a JavaScript Object and I’m sure the value of any key is an array (even empty in some case):

{key1:["a","b","c"],key2:["d","e","f"],key3:...}

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

var obj = {key1:["a","b","c"],key2:["d","e","f"]};

var arr = Object.keys(obj).reduce(function(res, v) {
    return res.concat(obj[v]);
}, []);

// ["a", "b", "c", "d", "e", "f"]
Advertisement