javascript - ES5 风格的 Concat 数组。将数组合并为一个
问题描述
我必须将数组块合并到带有 Angularjs 中对象数组的单个数组中。
我的输入数组将是:
[
[
{
"title": "asd",
"description": "asd"
},
{
"title": "asz",
"description": "sd"
}
],
[
{
"title": "ws",
"description": "sd"
},
{
"title": "re",
"description": "sd"
}
],
[
{
"title": "32",
"description": "xxs"
},
{
"title": "xxc",
"description": "11"
}
]
]
上面的输入数组应该像下面的对象数组一样保存
[
{
"title": "asd",
"description": "asd"
},
{
"title": "asz",
"description": "sd"
},
{
"title": "ws",
"description": "sd"
},
{
"title": "re",
"description": "sd"
},
{
"title": "32",
"description": "xxs"
},
{
"title": "xxc",
"description": "11"
}
]
我这样做了,如下所示,
const input=[[{"title":"asd","description":"asd"},{"title":"asz","description":"sd"}],[{"title":"ws","description":"sd"},{"title":"re","description":"sd"}],[{"title":"32","description":"xxs"},{"title":"xxc","description":"11"}]]
const output = [].concat(...input);
console.log(output);
但我认为它在 ES6 中。你能帮我实现 ES5 吗?
提前致谢
解决方案
您可以apply
输入数组数组concat
:
var input = [
[
{
"title": "asd",
"description": "asd"
},
{
"title": "asz",
"description": "sd"
}
],
[
{
"title": "ws",
"description": "sd"
},
{
"title": "re",
"description": "sd"
}
],
[
{
"title": "32",
"description": "xxs"
},
{
"title": "xxc",
"description": "11"
}
]
];
var result = Array.prototype.concat.apply([], input);
console.log(result);
推荐阅读
- mysql - 存储库对象与 DTO 对象的映射
- c# - C# RabbitMQ - 如何知道所有消息何时已被确认
- python - Scrapy + pyqt5:信号仅适用于主线程错误
- php - 数据表 - 排序和分页
- c++ - 使用单个变量计算字符输入
- openebs - OpenEBS 中的数据存储在哪里?
- android - 在 oreo 8.1.0 中我无法获取 fcm 设备 ID
- java - POST 请求与 POSTMAN/Advance rest 客户端一起正常工作,同时使用 HttpUrlConnection 给出 400 错误
- firebase - 无法访问 Firestore 云函数中的 Firestore 文档
- php - 在 React Native 中将数据插入 MySQL 数据库时出现 JSON 解析错误