gpt4 book ai didi

javascript - 如何更改 json 结构使其看起来像另一个 json 结构

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:10 25 4
gpt4 key购买 nike

我想更改我的 json 结构,我该怎么做?

我得到一个如下所示的 json:

 body: {
"111111": {
"name": "exp1",
"status": 10000
},
"222222": {
"name": "exp2",
"status": 20000
},
"333333": {
"name": "exp3",
"status": 30000
}
}

但我需要这种结构:

 body: {
bulks: [{
"id": "111111",
"name": "exp1",
"status": 100000
}, {
"id": "222222",
"name": "exp2",
"status": 200000
}, {
"id": "333333",
"name": "exp3",
"status": 300000
}]
}

因为在我的 html 中我想这样阅读它:

<div *ngIf="showingList">
<div class="list-bg" *ngFor="#bulk of listBulks | async">
ID: {{bulk.id}} name of item: {{bulk.name}}
</div>
</div>

最佳答案

将 Object#entries 和 Array#map 与扩展运算符一起使用。

const data={body:{111111:{name:"exp1",status:1e4},222222:{name:"exp2",status:2e4},333333:{name:"exp3",status:3e4}}};


const res = {body:{bulk:Object
.entries(data.body)
.map(a=>({id: a[0], ...a[1]}))}};

console.log(res);

关于javascript - 如何更改 json 结构使其看起来像另一个 json 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38567187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com