Javascript 将单个对象数组的数组转换为对象数组
我目前有一个数组,如下所示: 常量可写=[ [{id:1,name:item1,write:true}], [{id:3,name:item3,write:true}] ] 我想将所有包含单个对象的数组转换为对象数组 我尝试过通过可写数组进行映射,并将每个项推送到一个新数组中,但由于.map返回一个新数组,因此我得到了相同的结果。有没有办法做到这一点,或者这是不可能的 预期产出: 常数newArray=[ {id:1,name:item1,write:true}, {id:3,name:item3,write:true} ] 常量可写=[ [{id:1,name:item1,write:true}], [{id:3,name:item3,write:true}] ]; 让结果=writeable.flat;Javascript 将单个对象数组的数组转换为对象数组,javascript,arrays,Javascript,Arrays,我目前有一个数组,如下所示: 常量可写=[ [{id:1,name:item1,write:true}], [{id:3,name:item3,write:true}] ] 我想将所有包含单个对象的数组转换为对象数组 我尝试过通过可写数组进行映射,并将每个项推送到一个新数组中,但由于.map返回一个新数组,因此我得到了相同的结果。有没有办法做到这一点,或者这是不可能的 预期产出: 常数newArray=[ {id:1,name:item1,write:true}, {id:3,name:item
console.logresult 您需要返回正在迭代的元素中的对象
writeable.map(function (item) {
return item[0];
});
就用吧
常量可写=[
[{id:1,name:item1,write:true}],
[{id:3,name:item3,write:true}]
]
const res=可写的.flat//默认深度为1
console.logres;请添加想要的结果。您需要使用.flatMap而不是.map。或者只需writeable.flat:-[].concat.apply[],writeable,甚至[]。concat…writeable请共享预期的输出,只需使用.flat。是的,你是对的。我觉得太复杂了。编辑它。