我想重新映射一个JS数组。像这样的东西
var data = [
{
description: "bla",
value: 234,
}
{
description: "bla",
value: 234,
},
{
description: "bla",
value: 234,
},
{
description: "bla",
value: 234,
},
{
description: "bla",
value: 234,
},
]
应重新映射到2个单个数组。一个数组(descriptions
)应包含所有描述值,另一个数组(values
)应包含所有值。
我认为在JavaScript中有一个内置方法,但我不知道它是如何命名的。
答案 0 :(得分:2)
我认为这就是你想要的:
var data = [{description:"bla",value:234},{description:"bla",value:234},{description:"bla",value:234},{description:"bla",value:234},{description:"bla",value:234}]
.reduce((arr, x) => {
arr.values.push(x.value)
arr.descriptions.push(x.description)
return arr
}, { values: [], descriptions: [] })
console.log(data)
答案 1 :(得分:0)
“应重新映射到2个单个数组。一个数组(描述)应包含所有描述值,其他数组(值)应包含所有值。”
这是您想要的解决方案:
var data = [{description:"bla",value:234},{description:"bla",value:234},{description:"bla",value:234},{description:"bla",value:234},{description:"bla",value:234}],
descriptions = [],
values = [];
data.forEach(function(v){
descriptions.push(v.description);
values.push(v.value);
})
console.log(descriptions);
console.log(values);
答案 2 :(得分:-1)
var arrDescription = [];
var arrValue = [];
Object.keys(data).forEach(function(key) {
//get the value of name
//push the name string in the array
arrDescription.push(data[key]["description"]);
arrValue.push(data[key]["value"]);
});