我有从 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);
但这只会将值返回到新数组中。如何创建一个具有不同键但值来自原始数组的新数组?
最佳答案
只需从 map 中为每个项目返回一个新对象。
let result = data.entities.map(a => ({imgsrc: a.documentbody, imgDesc: a.filename}));
关于javascript - 将值和键从一个数组复制到另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57515477/