【发布时间】:2018-03-08 14:42:54
【问题描述】:
我遇到了扁平化数组的问题。
鉴于结构如下
{
"name": "Somename",
"property": [
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
}
]
]
}
如何将匹配项扁平化为单个对象数组或实际上具有扁平结构,其中所有嵌套项都在单个数组或对象中?
到目前为止,我已经设法通过整理数据取得了进展,但似乎卡在了这一点上,我几乎可以使用任何库或工具以及最新的 JS 功能。
我尝试过映射值,使用 lodash deepMerge 减少它,但似乎无法完成我想要的。
输入:
const data = [
{
"sport": "Handball",
"matches": [
[
{
"home": "Izmir BSB SK (Youth) (Wom)",
"away": "Bursa Osmangazi (Youth) (Wom)",
"ID": "3092996854"
}
],
[
{
"home": "Al Muheet U21",
"away": "Al Mohmel U21",
"ID": "3092999932"
}
]
]
},
{
"sport": "Volleyball",
"matches": [
[
{
"home": "Ji-Hee Choi/Michika Ozeki",
"away": "Panji Ahmad Maulana",
"ID": "3093062401"
},
{
"home": "Ryazan (Wom)",
"away": "Angara Irkutsk (Wom)",
"ID": "3093062393"
}
],
[
{
"home": "CF Carthage (Wom)",
"away": "Winner - Broughton Excels",
"ID": "3093721823"
}
],
[
{
"home": "Ankara Yildirim Beyazit Universitesi (Wom)",
"away": "Saglik Bilimleri Universitesi (Wom)",
"ID": "3093058567"
}
]
]
}
]
每个匹配道具的预期输出:
{
"sport": "Handball",
"matches": [
{home: '...', other: '...', id: '...'},
{home: '...', other: '...', id: '...'},
{home: '...', other: '...', id: '...'},
{home: '...', other: '...', id: '...'},
]
}
【问题讨论】:
-
您的预期输出是什么?
[{.?.},{.?.}] -
{ "name": "Somename", "property": [ {prop: 'someProp', other: 'otherProp'}, {prop: 'someProp', other: 'otherProp'}, {prop:'someProp',其他:'otherProp'},{prop:'someProp',其他:'otherProp'}] }
-
将其添加到您的问题中
-
Or maybe better illustrated with this pic- 不。我们讨厌图片;/ -
@georg 有照片吗?我看不到它们,.. :/ - 我想这就是为什么这可能适用,即使它本身不是代码Why not to upload Images of Code when asking a Question - 另外,谁知道该图像链接是否在 2 个月内仍然存在,使如果问题消失并且预期的输出不在问题中,则该问题对未来的用户毫无用处。
标签: javascript arrays object ecmascript-6 lodash