猿问

对象中的深度收集

我在下面有一个 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"

  },


繁星淼淼
浏览 147回答 4
4回答

慕尼黑8549860

您可以使用 提取数组 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);

慕桂英4014372

您首先需要将 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; }

函数式编程

由于这个问题被标记为 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"}]}}},&nbsp; &nbsp; res = _.flatMapDeep(input, e => Object.values(e.meta));&nbsp; &nbsp;&nbsp;console.log(res);<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

蛊毒传说

let arr = Object.values(obj)let ids = [];for(let i = 0; i < arr.length; i++){&nbsp; for(let j = 0; j < Object.values(arr[i].meta).length; j++){&nbsp; &nbsp; for(let k = 0; k < Object.values(arr[i].meta)[j].length; k++){&nbsp; &nbsp; &nbsp; ids.push(Object.values(arr[i].meta)[j][k].id)&nbsp; &nbsp; }&nbsp; }}console.log([...new Set(ids)])这将返回所有唯一 ID
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答