3 回答
TA贡献1846条经验 获得超7个赞
使用map和解构
const data = [
{ genericName: "genesName test", genericId: 3, code: "generics" },
{ genericName: "genesName fhfghsd", genericId: 8, code: "generics" },
{ genericName: "genesName fhld", genericId: 11, code: "generics" },
];
const res = data.map(({ code, genericId }) => ({ [code]: { genericId } }));
console.log(res);
TA贡献2011条经验 获得超2个赞
您可以尝试map以下方法Array:
const obj = [{
"genericName": "genesName test",
"genericId": 3,
"code": "generics"
}, {
"genericName": "genesName fhfghsd",
"genericId": 8,
"code": "generics"
}, {
"genericName": "genesName fhld",
"genericId": 11,
"code": "generics"
}]
const mapObjToResult = (item, idx, arr) => {
return {
[item.code]: {
genericId: item.genericId
}
}
}
const result = obj.map(mapObjToResult);
console.log(result);
TA贡献1794条经验 获得超8个赞
如果你想要对象数组,那么这个呢?
let gens = [
{"genericName":"genesName test","genericId":3,"code":"generics"},
{"genericName":"genesName fhfghsd","genericId":8,"code":"generics"},
{"genericName":"genesName fhld","genericId":11,"code":"generics"}
]
result = gens.map(item => ({ [item.code] : {genericId: item.genericId}})
添加回答
举报