如何解决将单个对象数组的数组转换为对象数组
我目前有一个数组,如下所示:
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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。