第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對象中的深度收集

對象中的深度收集

繁星淼淼 2022-10-27 10:49:23
我在下面有一個 JSON 對象:{    "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"              }          ]      }  }}使用 Lodash 或 VanilaJS 的最佳方式返回(唯一id):[  {      "id": "math_point",      "name": "Math Point",      "type": "comparable"  },  {      "id": "math_switch",      "name": "Math Switch",      "type": "switch"  },  {      "id": "history_point",      "name": "Math Point",      "type": "comparable"  },  {      "id": "history_switch",      "name": "Math Switch",      "type": "switch"  },
查看完整描述

4 回答

?
慕尼黑8549860

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);


查看完整回答
反對 回復(fù) 2022-10-27
?
慕桂英4014372

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; }


查看完整回答
反對 回復(fù) 2022-10-27
?
函數(shù)式編程

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>


查看完整回答
反對 回復(fù) 2022-10-27
?
蠱毒傳說

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


查看完整回答
反對 回復(fù) 2022-10-27
  • 4 回答
  • 0 關(guān)注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號