简体   繁体   English

使用标识符遍历JSON对象并使用标识符获取特定值

[英]With an identifier traverse through a JSON object and get a specific value with the identifier

Traverse through a JSON object which has nested arrays objects inside it . 遍历其中包含嵌套数组对象的JSON对象。 The label value is provided which is the identifier with which need to return the associated level metrics value . 提供了标签值,该标签值是需要返回相关级别指标值的标识符。 If the label is found in the 2nd level find the metrics at the second level and it should be returned 如果在第二层找到标签,则在第二层找到指标,并应将其返回

I couldn't get the logic on how to traverse through an object and return the specific value 我无法获得关于如何遍历对象并返回特定值的逻辑

function getMetrics(arr, label) {

  for (let i = 0; i < arr.length; i++) {
    if (arr[i].label === label) {
      return arr[i].metricsValue;
    } else if (arr[i].children) {
      return getMetrics(arr[i].children, label);
    }
  }

  return "Not found";
}
const selectedMetrics = getMetrics(dataObj.series, '1');

Consider the JSON object with children specifies the sub level of the current level . 考虑带有对象的JSON对象,该子对象指定当前级别的子级别。

const dataObj = {
  series: [
    {
      label: "A",
      metricsValue: "ma",
      children: [
        {
          label: "A-B",
          value: 6,
          metricsValue: "ma-mb"
        },
        {
          label: "A-B-C",
          metricsValue: "ma-mb-mc",
          children: [
            {
              label : "A-B-C-D",
              value: 6,
              metricsValue: "ma-mb-mc-md"
            }
          ]
        }
      ]
    },
    {
      label: "1",
      metricsValue: "m1",
    }
  ]
};

Expected Result : When the input is "1", it should return 预期结果:输入为“ 1”时,应返回

selectedMetrics= "m1"

Input : "ABCD" 输入:“ ABCD”

selectedMetrics= "ma-mb-mc-md"

You can perform a Depth first search (DFS) or Breadth first search (BFS) to find metricValues at any level. 您可以执行深度优先搜索 (DFS)或宽度优先搜索 (BFS)来查找任何级别的metricValues

Here I'm using DFS to find the required value. 在这里,我使用DFS查找所需的值。 This works for data with any nested levels. 这适用于具有任何嵌套级别的数据。

 const dataObj = { series: [ { label: "A", metricsValue: "ma", children: [ { label: "AB", value: 6, metricsValue: "ma-mb" }, { label: "ABC", metricsValue: "ma-mb-mc", children: [ { label: "ABCD", value: 6, metricsValue: "ma-mb-mc-md" } ] } ] }, { label: "1", metricsValue: "m1"} ] }; function getMetrics(arr, label) { var result; for (let i = 0; i < arr.length; i++) { if (arr[i].label === label) { return arr[i].metricsValue; } else if (arr[i].children) { result = getMetrics(arr[i].children, label); if (result) { return result; } } } return null; } console.log("selectedMetrics for 'A' = " + getMetrics(dataObj.series, 'A')); console.log("selectedMetrics for 'AB' = " + getMetrics(dataObj.series, 'A-B')); console.log("selectedMetrics for 'ABC' = " + getMetrics(dataObj.series, 'AB-C')); console.log("selectedMetrics for 'ABCD' = " + getMetrics(dataObj.series, 'ABC-D')); console.log("selectedMetrics for '1' = " + getMetrics(dataObj.series, '1')); 

Your'e passing in the value, so use it instead of the string & you're not accessing the children nodes. 您正在传递值,因此请使用它代替字符串,并且您不会访问子节点。

 for(var i=0; i< arr.length;i++){
        const x = arr[i];
        if (x.children.label === value) {
          console.log(x.metricValue)
        }else{
          x.forEach(element => {
            if (element.children.label === value) {
              console.log(element.metricValue)
            }else{
              element.forEach(secondEl =>{
                if (secondEl.children.label === value) {
                  console.log(secondEl.metricValue)
                }
              })
            }
          });
        }
      }

You can create a more elegant way of iterating around the children nodes but that may help you out 您可以创建一种更优雅的方法来遍历子节点,但这可能会帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM