【问题标题】:Removing levels in nested objects in JavaScript在 JavaScript 中删除嵌套对象中的级别
【发布时间】:2020-07-24 12:40:33
【问题描述】:

我使用的 REST API 返回此数据格式:

const documents = [
  {
    "fields": {
      "title": {
        "stringValue": "77"
      },
      "difficulty": {
        "doubleValue": 77
      },
    },
    "createTime": "2020-04-10T15:13:47.074204Z"
  },
  {
    "fields": {
      "title": {
        "stringValue": "99"
      },
      "difficulty": {
        "doubleValue": 99
      },
    },
    "createTime": "2020-04-10T15:13:47.074204Z"
  }
]

我需要的是这种格式:

{
  title: "77",
  difficulty: 77
},
{
  title: "99",
  difficulty: 99
}

这意味着我不仅要对这些数据进行分组,还要完全删除中间的两层。我该怎么做?

作为奖励:我如何在这方面做得更好?有什么好的资源吗?

【问题讨论】:

    标签: javascript arrays object grouping nested-object


    【解决方案1】:

    通过使用.map() 并解构如下:

    const documents = [{ "fields": { "title": { "stringValue": "77" }, "difficulty": { "doubleValue": 77 }, }, "createTime": "2020-04-10T15:13:47.074204Z", }, { "fields": { "title": { "stringValue": "99" }, "difficulty": { "doubleValue": 99 }, }, "createTime": "2020-04-10T15:13:47.074204Z" } ];
    
    const result = documents.map(({fields}) => ({title: fields.title.stringValue, difficulty: fields.difficulty.doubleValue}));
    
    console.log(result);

    我希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您可以使用Array#map 方法和Object.values 方法来实现结果。

      const documents = [{
          "fields": {
            "title": {
              "stringValue": "77"
            },
            "difficulty": {
              "doubleValue": 77
            },
          },
          "createTime": "2020-04-10T15:13:47.074204Z"
        },
        {
          "fields": {
            "title": {
              "stringValue": "99"
            },
            "difficulty": {
              "doubleValue": 99
            },
          },
          "createTime": "2020-04-10T15:13:47.074204Z"
        }
      ]
      
      const result = documents.map(({
        fields: {
          title: t,
          difficulty: d
        }
      }) => ({
        title: Object.values(t)[0],
        difficulty: Object.values(d)[0]
      }));
      
      console.log(result);

      【讨论】:

      • 非常感谢大家 - 已解决。所有解决方案都运行良好!
      【解决方案3】:

      您必须遍历数据并选择您的字段。一种描述性的方法是使用数组上可用的map() 函数。

      const documents = [
        {
          "fields": {
            "title": {
              "stringValue": "77"
            },
            "difficulty": {
              "doubleValue": 77
            },
          },
          "createTime": "2020-04-10T15:13:47.074204Z"
        },
        {
          "fields": {
            "title": {
              "stringValue": "99"
            },
            "difficulty": {
              "doubleValue": 99
            },
          },
          "createTime": "2020-04-10T15:13:47.074204Z"
        }
      ]
      
      let result = documents.map(document => ({
        title: document.fields.title.stringValue,
        difficulty: document.fields.difficulty.doubleValue
      }));
      
      console.log(result);

      【讨论】:

        猜你喜欢
        • 2016-04-09
        • 2018-04-28
        • 2020-12-07
        • 2019-12-13
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 2020-08-02
        相关资源
        最近更新 更多