4 回答
TA贡献1818条经验 获得超11个赞
您可以使用 提取数组 Object.values。将它们展平为一个数组,然后使用一个 Map,键为id,将其减少为一组唯一的值:
let obj = {"JOHN": {"class": "ABC","meta": {"Math": [{"id": "math_point","name": "Math Point","type": "comparable"},{"id": "math_switch","name": "Math Switch","type": "switch"}],"History": [{"id": "history_point","name": "Math Point","type": "comparable"},{"id": "history_switch","name": "Math Switch","type": "switch"}]}},"BOB": {"class": "DFE","meta": {"Math": [{"id": "math_point","name": "Math Point","type": "comparable"},{"id": "math_switch","name": "Math Switch","type": "switch"}],"Biology": [{"id": "biology_point","name": "Biology Point","type": "comparable"},{"id": "biology_switch","name": "Biology Switch","type": "switch"}]}}};
let arr = Array.from(new Map(
Object.values(obj)
.flatMap(({meta}) => Object.values(meta))
.flat()
.map(o => [o.id, o])
).values());
console.log(arr);
TA贡献1871条经验 获得超13个赞
您首先需要将 JSON 解析为 javascript 对象,然后您可以使用临时对象来保存所有唯一键及其对应的对象。之后,您可以使用Object.values()函数从临时对象中提取对象
const obj = {"JOHN": {"class": "ABC","meta": {"Math": [{"id": "math_point","name": "Math Point","type": "comparable"},{"id": "math_switch","name": "Math Switch","type": "switch"}],"History": [{"id": "history_point","name": "Math Point","type": "comparable"},{"id": "history_switch","name": "Math Switch","type": "switch"}]}},"BOB": {"class": "DFE","meta": {"Math": [{"id": "math_point","name": "Math Point","type": "comparable"},{"id": "math_switch","name": "Math Switch","type": "switch"}],"Biology": [{"id": "biology_point","name": "Biology Point","type": "comparable"},{"id": "biology_switch","name": "Biology Switch","type": "switch"}]}}};
const temp = {};
Object.values(obj).forEach(({meta}) => {
Object.values(meta).flat().forEach(o => (temp[o.id] = o));
});
const result = Object.values(temp);
console.log(result);
.as-console-wrapper { max-height: 100% !important; }
TA贡献1807条经验 获得超9个赞
由于这个问题被标记为 lodash 怎么样只是简单地使用这个:
_.flatMapDeep(input, e => Object.values(e.meta))
它更具可读性。
由于您只对每个元对象的值感兴趣。每个元对象的所有值都是一个数组(值,而不是键),您基本上是在查看这些数组的内容,就是这样。所以只需简单地把它们都拿走,然后用 lodash 压平。
让我们有一个片段:
let input = {"JOHN":{"class":"ABC","meta":{"Math":[{"id":"math_point","name":"Math Point","type":"comparable"},{"id":"math_switch","name":"Math Switch","type":"switch"}],"History":[{"id":"history_point","name":"Math Point","type":"comparable"},{"id":"history_switch","name":"Math Switch","type":"switch"}]}},"BOB":{"class":"DFE","meta":{"Math":[{"id":"math_point","name":"Math Point","type":"comparable"},{"id":"math_switch","name":"Math Switch","type":"switch"}],"Biology":[{"id":"biology_point","name":"Biology Point","type":"comparable"},{"id":"biology_switch","name":"Biology Switch","type":"switch"}]}}},
res = _.flatMapDeep(input, e => Object.values(e.meta));
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
TA贡献1895条经验 获得超3个赞
let arr = Object.values(obj)
let ids = [];
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < Object.values(arr[i].meta).length; j++){
for(let k = 0; k < Object.values(arr[i].meta)[j].length; k++){
ids.push(Object.values(arr[i].meta)[j][k].id)
}
}
}
console.log([...new Set(ids)])
这将返回所有唯一 ID
添加回答
举报
