我有一个带动态键的对象数组
response = [{"1": 1}, {"2": 1}, {"3": 1}, {"4": 0}, {"5": 0}];
Run Code Online (Sandbox Code Playgroud)
我想将这个对象数组展平为一个数组
Output = [1, 1, 1, 0, 0]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
const test2 = this.response.map(obj => {
Object.keys(obj).map(function(key){
return obj[key]
})
});
const test = this.response.reduce(function(prev, curr){
console.log(curr)
return (curr) ? prev.concat(curr): prev;
},[]);
Run Code Online (Sandbox Code Playgroud)
您可以使用map和object.values
response = [{"1": 1}, {"2": 1}, {"3": 1}, {"4": 0}, {"5": 0}]
const vals = response.map(o => Object.values(o)[0])
console.log(vals)Run Code Online (Sandbox Code Playgroud)
你可以用.map()与.concat():
let data = [{"1": 1}, {"2": 1}, {"3": 1}, {"4": 0}, {"5": 0}];
let result = [].concat(...data.map(Object.values));
console.log(result);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
96 次 |
| 最近记录: |