连接对象值
Concatenate Object values
我有一个 JavaScript 对象,我确定任何键的值都是一个数组(在某些情况下甚至是空的):
{key1:["a","b","c"],key2:["d","e","f"],key3:...}
除了使用 Underscore 之外,还有什么方法可以连接此对象的所有值(并创建一个新数组)?
目前我使用 Object.keys
获取键名,然后循环并连接。
感谢任何帮助。
检查array 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:
试试这个:
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);
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"]
一种简单的方法是使用 Object.values()
and concatenate 和 [].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)
另一种类似的方式,是合并 Object.values()
by spreading them into 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)
也减少 Object.values()
and concatenate 它们的每个值,你可以得到相同的结果:
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)
要完成,您也可以使用Array.prototype.flat()
over each value of the 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)
希望这些方法可以帮助到外面的人:)
我有一个 JavaScript 对象,我确定任何键的值都是一个数组(在某些情况下甚至是空的):
{key1:["a","b","c"],key2:["d","e","f"],key3:...}
除了使用 Underscore 之外,还有什么方法可以连接此对象的所有值(并创建一个新数组)?
目前我使用 Object.keys
获取键名,然后循环并连接。
感谢任何帮助。
检查array 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:
试试这个: 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);
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"]
一种简单的方法是使用 Object.values()
and concatenate 和 [].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)
另一种类似的方式,是合并 Object.values()
by spreading them into 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)
也减少 Object.values()
and concatenate 它们的每个值,你可以得到相同的结果:
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)
要完成,您也可以使用Array.prototype.flat()
over each value of the 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)
希望这些方法可以帮助到外面的人:)