我有一个对象数组,每个对象如下所示(轮询响应):
{
"slug": "18-AZ-Gov-GE-DvF",
"name": "2018 Arizona Gubernatorial GE",
"tags": [],
"charts": [],
"election_date": "2018-11-06",
"n_polls": 1,
"created_at": "2017-06-13T13:32:26.000Z",
"responses": [
{
"label": "Ducey",
"name": "Doug Ducey",
"party": "Republican",
"incumbent": true
},
{
"label": "Farley",
"name": "Steve Farley",
"party": "Democrat",
"incumbent": false
},
{
"label": "Other",
"name": "Other",
"party": null,
"incumbent": false
},
{
"label": "Undecided",
"name": "Undecided",
"party": null,
"incumbent": false
}
]
},
我需要展平responses键访问的数组,以便每个对象都键入它的迭代器。
最终对象应如下所示:
{
"slug": "18-AZ-Gov-GE-DvF",
"name": "2018 Arizona Gubernatorial GE",
"tags": [],
"charts": [],
"election_date": "2018-11-06",
"n_polls": 1,
"created_at": "2017-06-13T13:32:26.000Z",
"label1": "Ducey",
"name1": "Doug Ducey",
"party1": "Republican",
"incumbent1": true
"label2": "Farley",
"name2": "Steve Farley",
"party2": "Democrat",
"incumbent2": false
"label3": "Other",
"name3": "Other",
"party3": null,
"incumbent3": false
"label4": "Undecided",
"name4": "Undecided",
"party4": null,
"incumbent4": false
},
我看到的答案在展平或在集合上执行时不会重命名对象键。
我尝试了几种解决方案,但想在真正深入研究之前看看是否有简单的 es6 方法。