【问题标题】:JSON nested object reduce or flattenJSON 嵌套对象减少或展平
【发布时间】:2021-09-09 05:52:00
【问题描述】:

我需要将这个 JSON 展平,使其没有巢,只有贝类,然后是它的名称等。提前致谢!我尝试了一些东西,所以也许可以减少或映射,但我对那些解决问题的知识还不够了解,你们可以分享任何专门针对此类问题的在线资源吗?我经常使用这些,掌握起来会非常方便!

"data": [
        {
            "name": "Batch1",
            "description": "seed",
            "age": 2,
            "quantity": 1000,
            "source": "Hatchery",
            "hatchery": "robs hatchery",
            "location": "dingle",
            "shellfish": {
                "name": "oyster"
            },
            "grade_list": {
                "name": "Grade0"
            },
            "stock_type": {
                "name": "seeds"
            }
        },
        {
            "name": "Batch2",
            "description": "Batch2",
            "age": 20,
            "quantity": 15700,
            "source": "aka",
            "hatchery": "aka",
            "location": "dingle",
            "shellfish": {
                "name": "oyster"
            },
            "grade_list": {
                "name": "Grade1"
            },
            "stock_type": {
                "name": "mature"
            }
        },
        {
            "name": "5555",
            "description": "45",
            "age": 1,
            "quantity": 134,
            "source": "fhh",
            "hatchery": "hfhj",
            "location": "garden",
            "shellfish": {
                "name": "oyster"
            },
            "grade_list": {
                "name": "Grade0"
            },
            "stock_type": {
                "name": "seeds"
            }
        }
    ]

编辑:我需要这个没有巢,看起来像这样:

"data": [
        {
            "name": "Batch1",
            "description": "seed",
            "age": 2,
            "quantity": 1000,
            "source": "Hatchery",
            "hatchery": "robs hatchery",
            "location": "dingle",
            "shellfish": {
                "name": "oyster"
            },
            "grade_list": {
                "name": "Grade0"
            },
            "stock_type": {
                "name": "seeds"
            }
        },
        {
            "name": "Batch2",
            "description": "Batch2",
            "age": 20,
            "quantity": 15700,
            "source": "aka",
            "hatchery": "aka",
            "location": "dingle",
            "shellfish": {
                "name": "oyster"
            },
            "grade_list": {
                "name": "Grade1"
            },
            "stock_type": {
                "name": "mature"
            }
        },
        {
            "name": "5555",
            "description": "45",
            "age": 1,
            "quantity": 134,
            "source": "fhh",
            "hatchery": "hfhj",
            "location": "garden",
            "shellfish": "oyster",
            "grade_list": "Grade0",
            "stock_type": "seeds"
        }
    ]

【问题讨论】:

  • 这是无效的 JSON。 JSON 不能以 "data": 开头。可能缺少一些大括号。此外,如果您在 JavaScript 中有这种数据结构,请不要将其标记为 json(阅读使用说明)。
  • “提前致谢”:等等……有什么问题?您可以编辑您的问题并添加您的尝试以及您遇到的问题吗?
  • 预期输出是什么?您的描述模棱两可:“我需要将这个 JSON 展平,使其没有巢,只有贝类,然后是它的名称等。”
  • 大家好,对不起,我现在要编辑 qs!
  • 抱歉现在发了2次模棱两可,没有答案不得不重新发布qs

标签: javascript json


【解决方案1】:

如果您想让数组中所有元素的这一过程更加自动化,您可以使用映射来更改所有具有 name 属性的元素。

const response = {
  "data": [
    {
      "name": "Batch1",
      "description": "seed",
      "age": 2,
      "quantity": 1000,
      "source": "Hatchery",
      "hatchery": "robs hatchery",
      "location": "dingle",
      "shellfish": {
        "name": "oyster"
      },
      "grade_list": {
        "name": "Grade0"
      },
      "stock_type": {
        "name": "seeds"
      }
    },
    {
      "name": "Batch2",
      "description": "Batch2",
      "age": 20,
      "quantity": 15700,
      "source": "aka",
      "hatchery": "aka",
      "location": "dingle",
      "shellfish": {
        "name": "oyster"
      },
      "grade_list": {
        "name": "Grade1"
      },
      "stock_type": {
        "name": "mature"
      }
    },
    {
      "name": "5555",
      "description": "45",
      "age": 1,
      "quantity": 134,
      "source": "fhh",
      "hatchery": "hfhj",
      "location": "garden",
      "shellfish": {
        "name": "oyster"
      },
      "grade_list": {
        "name": "Grade0"
      },
      "stock_type": {
        "name": "seeds"
      }
    }
  ]
}

const result = response.data.map((element) => {
  const keysWithNameProp = Object.keys(element).filter(key => element[key].name !== undefined);
  const copyOfElement = {...element};
  keysWithNameProp.forEach(prop => {
    copyOfElement[prop] = element[prop].name;
  })
  return copyOfElement;
});

console.log(result);

此代码是否首先要做的是获得具有name 属性的所有键的列表。现在有了这个列表,我可以遍历它并更改对象道具以仅使用 name 的值。

【讨论】:

  • 嘿,另一个问题,如果贝类是“物种”而不是名称,而另外两个是“名称”,我将如何调整函数以使这两个对象都变平?谢谢
  • @MichalKubiak 在这种情况下,您需要检查对象是否具有 namespecies 属性。您可以使用element[key].name !== undefined || element[key].species !== undefined 来实现此功能,即filter 回调。最后一件事是将forEach 中的属性值更改为element[prop].name || element[prop].species。完整代码:gist.github.com/mathiasgheno/7ce4e5c4323efb63e0a955191ce76c43
【解决方案2】:

这会将任何子对象扁平化为单个键值对; 在子对象具有包含预期值的单个键“名称”时才会起作用。代码中的注释解释了它是如何工作的:

const data = {
  "data": [{
      "name": "Batch1",
      "description": "seed",
      "age": 2,
      "quantity": 1000,
      "source": "Hatchery",
      "hatchery": "robs hatchery",
      "location": "dingle",
      "shellfish": {
        "name": "oyster"
      },
      "grade_list": {
        "name": "Grade0"
      },
      "stock_type": {
        "name": "seeds"
      }
    },
    {
      "name": "Batch2",
      "description": "Batch2",
      "age": 20,
      "quantity": 15700,
      "source": "aka",
      "hatchery": "aka",
      "location": "dingle",
      "shellfish": {
        "name": "oyster"
      },
      "grade_list": {
        "name": "Grade1"
      },
      "stock_type": {
        "name": "mature"
      }
    },
    {
      "name": "5555",
      "description": "45",
      "age": 1,
      "quantity": 134,
      "source": "fhh",
      "hatchery": "hfhj",
      "location": "garden",
      "shellfish": "oyster",
      "grade_list": "Grade0",
      "stock_type": "seeds"
    }
  ]
}

const transform = (input) => {
  // step through each object in the array
  for (obj of input.data) {
    // step through each key in the object
    for (k of Object.keys(obj)) {
      // if that key contains an object:
      if (typeof(obj[k])==='object') {
        // replace the object with its own 'name' field
        obj[k] = obj[k].name
      }
    }
  }
  return input
}

console.log(transform(data))

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 2012-05-29
    • 2020-03-02
    • 2018-10-24
    • 2019-12-21
    • 2019-01-17
    • 2019-01-26
    • 2020-01-21
    相关资源
    最近更新 更多