javascript - 将单个对象数组的数组转换为对象数组
问题描述
我目前有一个数组,如下所示:
const writeable = [
[{id: 1, name: "item1", write: true}],
[{id: 3, name: "item3", write: true}]
]
我想将数组数组(所有数组都包含一个对象)转换为对象数组。
我已经尝试通过writeable
数组进行映射并将每个项目推入一个新数组,但是因为.map
返回一个新数组,所以我得到了相同的结果。有没有办法做到这一点,或者这是不可能的?
预期输出:
const newArray = [
{id: 1, name: "item1", write: true},
{id: 3, name: "item3", 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);
推荐阅读
- macos - Quasar Electron 构建无法在具有 arm64 架构的 M1 mac mini 上运行
- powershell - 开始睡眠意外行为
- excel - 在Excel VBA上用引号打印单元格值的写入函数
- python - 如何读取文本文件中的特定行?
- javascript - 如何使用护照本地猫鼬更改密码?
- javascript - Jquery/JS - 根据值属性查找输入并触发点击
- android - Android Studio EditText 不能为空错误
- javascript - JS:我需要 3 个标题才能点击/标题变成蓝色。事件监听器问题
- flutter - 在颤振中自动检测我的应用是否收到短信
- c# - 如何捕获所有 Web api 响应的 HttpContext.Response.Headers?