嗨,我在解析数据时遇到了大问题。我有这样的结构:
<code>
[{
"absent": ["a", "b", "c"],
"against": ["a", "b", "c"],
"club": "club1",
"for": ["a", "b", "c"],
"withhold": ["a", "b", "c"]
}, {
"absent": ["a", "b", "c"],
"against": ["a", "b", "c"],
"club": "club2",
"for": ["a", "b", "c"],
"withhold": ["a", "b", "c"]
}, {
"absent": ["a", "b", "c"],
"against": ["a", "b", "c"],
"club": "club3",
"for": ["a", "b", "c"],
"withhold": ["a", "b", "c"]
}]
</code>
我需要将此结构解析为这样的数组:
<code>
array={a,"absent",clube1
b,absent,clube1
c,absent,clube1
a,against,clube1
b,against,clube1
c,against,clube1
a,for,clube1
b,for,clube1
c,for,clube1
a,withhold,clube1
b,withhold,clube1
c,withhold,clube1
a,absent,clube2
b,absent,clube2
c,absent,clube2
a,against,clube2
b,against,clube2
c,against,clube2
a,for,clube2
b,for,clube2
c,for,clube2
a,withhold,clube2
b,withhold,clube2
c,withhold,clube2
}
</code>
等等...有人可以帮我吗?我正在使用纯 Java 脚本。我无法改变这个结构,所以只有一种方式是编写代码来解决这个问题。感谢您的帮助,并为我的英语感到抱歉。
这应该可以解决问题,它基本上映射了整个结构:
data.map((el) =>
Object.keys(el)
.filter((key) => key !== "club")
.map((key) => el[key].map((v) => [v, key, el["club"]]))
).join()
对于 OP 的其他格式:
data.map((el) =>
Object.keys(el)
.filter((key) => key !== "club")
.map((key) => el[key].map((v) => [v, key, el["club"]]))
).reduce((arr, el) => arr.concat(el.reduce((x, a) => x.concat(a), [])), [])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句