【问题标题】:How to extract both deeply and shallowly nested fields in array of json objects如何在 json 对象数组中提取深层和浅层嵌套的字段
【发布时间】:2020-02-27 17:34:29
【问题描述】:

假设我有这个 json 对象:

{
    "lotOfJson": [
    {
        "value": "someName",
        "property": "name",
        "children": [],
        "constraints": {
            "IsValidName": "name someName isn't valid"
        }
    },
    {
        "value": [
            {
                "id": "firstBadId"
            },
            {
                "id": "secondBadId"
            }
        ],
        "property": "listOfIds",
        "children": [
            {
                "value": {
                    "id": "firstBadId"
                },
                "property": "0",
                "children": [
                    {
                        "value": "firstBadId",
                        "property": "id",
                        "children": [],
                        "constraints": {
                            "badIdError": "This Id is bad!"
                        }
                    }
                ]
            },
            {
                "value": {
                    "id": "secondBadId"
                },
                "property": "1",
                "children": [
                    {
                        "value": "secondBadId",
                        "property": "id",
                        "children": [],
                        "constraints": {
                            "badIdError": "This Id is bad"
                        }
                    }
                ]
            }
        ]
    }
]

}

这个数组可以有深度嵌套的 JSON——没有办法知道有多深。

只要有一个看起来像这样的块:

"value": "",
"property": "",
"children": [],
"constraints": {
    "": ""
}

我想提取valuepropertyconstraints 的值并将它们保存在一个数组中。对于我上面的 "lotsOfJson" 示例,这看起来像: ["someName", "name", "name SomeName isn't valid", "firstBadId, "id", "This Id is bad!", "secondBadId, "id", "This Id is bad!"]

所以我只在它们平行时提取值、属性和约束,就像上面的块一样。

例如,我可以使用迭代器不可知地提取 JSON 数组中的所有 valuepropertyconstraints,但有没有办法仅在它们彼此平行时提取它们?

【问题讨论】:

  • JSON 是一种用于数据交换的文本符号(More here.) 如果您正在处理 JavaScript 源代码,而不是处理 string,那么您就不是在处理 JSON。
  • 您的源 JSON 格式不正确,并且您的目标属性(valuepropertyconstraints)不统一 - 它们可以采用字符串的值, 对象或数组在源的不同部分。如果这只是笨拙地尝试混淆源数据的结果,如果您需要获得相关答案,请制作一个更好的样本来玩。一般来说,简单的递归函数应该在这里工作。

标签: javascript json nested iterator data-extraction


【解决方案1】:

您可以使用for...in 循环为此创建递归函数,并且仅当当前对象在children 数组中没有元素时才添加到结果中。

const data = [{"value":"someName","property":"name","children":[],"constraints":{"IsValidName":"name someName isn't valid"}},{"value":[{"id":"firstBadId"},{"id":"secondBadId"}],"property":"listOfIds","children":[{"value":{"id":"firstBadId"},"property":"0","children":[{"value":"firstBadId","property":"id","children":[],"constraints":{"badIdError":"This Id is bad!"}}]},{"value":{"id":"secondBadId"},"property":"1","children":[{"value":"secondBadId","property":"id","children":[],"constraints":{"badIdError":"This Id is bad"}}]}]}]

function extract(data, fields) {
  let result = []

  for (let i in data) {
    if (typeof data[i] == 'object') {
      result.push(...extract(data[i], fields))
    }

    if (data.children && !data.children.length) {
      if (fields.includes(i)) {
        result = result.concat(
          typeof data[i] == 'object' ?
          Object.values(data[i]) :
          data[i]
        )
      }
    }
  }

  return result;
}

const result = extract(data, ['property', 'value', 'constraints'])
console.log(result)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多