为了账号安全,请及时绑定邮箱和手机立即绑定

对象中的深度收集

对象中的深度收集

繁星淼淼 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条经验 获得超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);


查看完整回答
反对 回复 2022-10-27
?
慕桂英4014372

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


查看完整回答
反对 回复 2022-10-27
?
函数式编程

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>


查看完整回答
反对 回复 2022-10-27
?
蛊毒传说

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


查看完整回答
反对 回复 2022-10-27
  • 4 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号