连接对象值 [英] Concatenate Object values

查看:67
本文介绍了连接对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript对象,我确定任何键的值都是一个数组(在某些情况下甚至是空的):

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:...}

除了使用Underscore之外,有没有办法连接这个Object的所有值(并创建一个新数组)?

Aside from using Underscore, is there any way to concatenate all the values of this Object (and create a new array)?

目前我使用 Object.keys 获取密钥名称,然后循环并连接。

At the moment I get the keys name using Object.keys, then I loop and concatenate.

感谢任何帮助。

推荐答案

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"]

这篇关于连接对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆