我有一个这样的json文件:
[
{
"stores": [
{
"name" : "My store",
"location" : "NY"
},
{
"name" : "Other store",
"location" : "FL"
},
{
"name" : "My other store",
"location" : "NY"
}
],
},
{
"stores": [
{
"name" : "Another My store",
"location" : "NY"
},
{
"name" : "Some Other store",
"location" : "FL"
},
{
"name" : "Secondary store",
"location" : "NY"
}
],
}
]
我想把数据合并成:
[
{
"name" : "My store",
"location" : "NY"
},
{
"name" : "Other store",
"location" : "FL"
},
{
"name" : "My other store",
"location" : "NY"
},
{
"name" : "Another My store",
"location" : "NY"
},
{
"name" : "Some Other store",
"location" : "FL"
},
{
"name" : "Secondary store",
"location" : "NY"
}
]
我从一个循环开始,但我不知道如何将它们全部分组为1:
const input = [
{
"stores": [
{
"name" : "My store",
"location" : "NY"
},
{
"name" : "Other store",
"location" : "FL"
},
{
"name" : "My other store",
"location" : "NY"
}
],
},
{
"stores": [
{
"name" : "Another My store",
"location" : "NY"
},
{
"name" : "Some Other store",
"location" : "FL"
},
{
"name" : "Secondary store",
"location" : "NY"
}
],
}
];
let grouped = []
input.forEach(item => {
const store = Object.keys(item.stores)
console.log(store)
})
2条答案
按热度按时间anhgbhbe1#
使用
Array#flatMap()
:试试看:
rqmkfv5c2#