javascript 连接对象值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28474049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:01:58  来源:igfitidea点击:

Concatenate Object values

javascriptarraysobjectconcat

提问by robe007

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

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

{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.

目前我使用 获得键名Object.keys,然后我循环并连接。

Any help is appreciated.

任何帮助表示赞赏。

回答by Aliaksei Shytkin

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

回答by robe007

A simple approach is to get the values using Object.values()and concatenatethem with [].concat.apply()in this way:

一种简单的方法是使用这些值Object.values()并以这种方式它们连接起来[].concat.apply()

const _obj = { key1:["a","b","c"], key2:["d","e","f"], key3:["g","h","i"] }

const _arr = [].concat.apply([], Object.values(_obj))

console.log(_arr)



Another similar way, is to mergeObject.values()by spreadingthem into Array.concat()like this:

另一种类似的方法是通过它们分散成这样来合并Object.values()Array.concat()

const _obj = { key1:["a","b","c"], key2:["d","e","f"], key3:["g","h","i"] }

const _arr = [].concat(...Object.values(_obj))

console.log(_arr)



Also reducingeach value of the Object.values()and concatenatethem, you can get the same result:

同样减少 的每个值Object.values()并将它们连接起来,您可以获得相同的结果:

const _obj = { key1:["a","b","c"], key2:["d","e","f"], key3:["g","h","i"] }

const _arr = Object.values(_obj).reduce((r,c) => r.concat(c), [])

console.log(_arr)



To finish, you can also use Array.prototype.flat()over each value of the Object.values(). Just keep in mind: it's not supported on all browsers.

最后,您还可以使用Array.prototype.flat()over 的每个值Object.values()。请记住:并非所有浏览器都支持它。

const _obj = { key1:["a","b","c"], key2:["d","e","f"], key3:["g","h","i"] }

const _arr = Object.values(_obj).flat()

console.log(_arr)

Hope this methods could help someone out there :)

希望这种方法可以帮助那里的人:)

回答by Ivan Sivak

Check the array concatfunction

检查数组 concat函数

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

var resultArray = [];
for (var key in obj) resultArray = resultArray.concat(obj[key]);

alert(resultArray);

jsfiddle:

jsfiddle:

http://jsfiddle.net/qpLq11ea/

http://jsfiddle.net/qpLq11ea/

回答by Rakesh_Kumar

Try this: http://jsfiddle.net/6hbp5bzo/

试试这个:http: //jsfiddle.net/6hbp5bzo/

var arr= [];
var o={key1:["a","b","c"],key2:["d","e","f"]}
for(key in o){
  if(o.hasOwnProperty(key)){
    arr.push(o[key]);
}
}
alert(arr);