问题描述
我目前有一个数组,如下所示:
const writeable = [
[{id: 1,name: "item1",write: true}],[{id: 3,name: "item3",write: true}]
]
我想将数组数组(所有数组都包含一个对象)转换为对象数组。
我尝试通过writeable
数组进行映射并将每个项目推入一个新数组,但是由于.map
返回一个新数组,因此得到的结果相同。有没有办法做到这一点,还是不可能?
预期输出:
const newArray = [
{id: 1,write: true},{id: 3,write: true}
]
解决方法
只需使用Array#flat
。
const writeable = [
[{id: 1,name: "item1",write: true}],[{id: 3,name: "item3",write: true}]
]
const res = writeable.flat();//default depth is 1
console.log(res);
,
您需要在要迭代的元素内返回对象
ggplot(mtcars,aes(x=hp,color=factor(cyl)))+
geom_density()+
scale_color_discrete(name="Cylinders",breaks=c(8,6,4),labels=c("8 Cylinder","6 Cylinder","4 Cylinder"))
,
const writeable =[
[{id: 1,write: true}]
];
let result = writeable.flat();
console.log(result);