4 回答

TA貢獻1818條經(jīng)驗 獲得超11個贊
您可以使用 提取數(shù)組 Object.values。將它們展平為一個數(shù)組,然后使用一個 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條經(jīng)驗 獲得超13個贊
您首先需要將 JSON 解析為 javascript 對象,然后您可以使用臨時對象來保存所有唯一鍵及其對應(yīng)的對象。之后,您可以使用Object.values()函數(shù)從臨時對象中提取對象
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條經(jīng)驗 獲得超9個贊
由于這個問題被標(biāo)記為 lodash 怎么樣只是簡單地使用這個:
_.flatMapDeep(input, e => Object.values(e.meta))
它更具可讀性。
由于您只對每個元對象的值感興趣。每個元對象的所有值都是一個數(shù)組(值,而不是鍵),您基本上是在查看這些數(shù)組的內(nèi)容,就是這樣。所以只需簡單地把它們都拿走,然后用 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條經(jīng)驗 獲得超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
添加回答
舉報