javascript - 将值和键从一个数组复制到另一个数组
问题描述
我有看起来像这样从 api 返回的数据
var data = {
"entities": [
{
"documentbody": Base64String,
"filename": "http-status-code-cheat-sheet1.png",
"filesize": 204326,
"mimetype": "image/png",
},
{
"documentbody": null,
"filename": "http-status-code-cheat-sheet2221.png",
"filesize": 204326,
"mimetype": "image/png",
}
]
}
我想创建一个看起来像这样的新数组
var images = [
{ imgsrc:(documentBodyValue),imgDesc:(fileNameValue)},
{imgsrc:(documentBodyValue),imgDesc:(fileNameValue)}
]
我曾尝试使用 map 函数let result = data.entities.map(a => a.filename);
,但这只会将值返回到新数组中。如何创建具有不同键但值来自原始数组的新数组?
解决方案
只需从地图中为每个项目返回一个新对象。
let result = data.entities.map(a => ({imgsrc: a.documentbody, imgDesc: a.filename}));
推荐阅读
- c++ - 如何 push_back 到带有 getter 函数的列表?
- bash - 将具有扩展名的文件从目录的子目录移动到与另一个目录具有相同名称的另一个子目录
- reactjs - 在电视上的 TizenBrowser v1.1.2.07 上运行反应网站时出现问题
- angular - Angular 4 datepicker 0 到 1 自动选择
- python - 使用python将数据字符串到列中
- rabbitmq - RabbitMQ - 需要关闭所有连接,但 close_all_connections 不起作用
- git - Git 存储库太大,即使在删除所有旧提交后也是如此
- php - 如何根据php laravel中的下拉值订购表格
- java - 从二维数组中查找不同大小的相邻元素
- python - 如何在colab中解压缩图像文件夹