具有长度未知的数据结构

时间:2019-05-14 12:00:14

标签: javascript

如何动态打印?

data = {
    "name": "EU",
    "size": 10,
    "nodes": [
        {
            "name": "England",
            "size": 2,
            "nodes": [
                {
                "name": "Center",
                "size": 1,
                "nodes": [
                        {
                        "name": "main street",
                        "size": 0.5,
                        "nodes": []
                        }
                    ]
                },
                {
                "name": "Vilage",
                "size": 1,
                "nodes": []
                }
            ]
        },
        {
            "name": "Germany",
            "size": 4,
            "nodes": []
        }
    ]
}

我需要以某种方式动态打印它

EU 10
EU - England 2
EU - England - Center 1
EU - England - Center - main street 0.5
EU - England - Vilage 1
EU - Germany 4 

这是我最需要的代码

var data = { "name": "EU", "size": 10, "nodes": [ { "name": "England", "size": 2, "nodes": [ { "name": "Center", "size": 1, "nodes": [ { "name": "main street", "size": 0.5, "nodes": [] } ] }, { "name": "Vilage", "size": 1, "nodes": [] } ] }, { "name": "Germany", "size": 4, "nodes": [] } ] }


function printValues(obj) {
  for (var key in obj) {
    if (typeof obj[key] === "object") {
      printValues(obj[key]);
    } else {
      console.log(obj[key]);
    }
  }
}
printValues(data)

这就是我得到的

5 个答案:

答案 0 :(得分:7)

您处在正确的轨道上-您确实可以使用递归遍历数据结构。您还需要获取name属性,并从遇到的每个name开始建立每一行的开头。您可以使用第二个参数将信息传递给递归函数。然后,您需要递归地为每个子节点调用该函数,并传递到目前为止遇到的所有name

const data = { "name": "EU", "size": 10, "nodes": [ { "name": "England", "size": 2, "nodes": [ { "name": "Center", "size": 1, "nodes": [ { "name": "main street", "size": 0.5, "nodes": [] } ] }, { "name": "Vilage", "size": 1, "nodes": [] } ] }, { "name": "Germany", "size": 4, "nodes": [] } ] }


function printValues(data, prefix = []) {
  //make a copy of the prefix instead of mutating it
  let location = prefix.concat(data.name);
  console.log(location.join(" - "), data.size);

  data.nodes.forEach(child => printValues(child, location))
}

printValues(data)

答案 1 :(得分:2)

这就是我得到的

var data = { "name": "EU", "size": 10, "nodes": [ { "name": "England", "size": 2, "nodes": [ { "name": "Center", "size": 1, "nodes": [ { "name": "main street", "size": 0.5, "nodes": [] } ] }, { "name": "Vilage", "size": 1, "nodes": [] } ] }, { "name": "Germany", "size": 4, "nodes": [] } ] }

function printElem(str, element) {
  ret = str + " " + element.name + " " + element.size;
  console.log(ret);
  for (item of element.nodes) {
    printElem(str + " " + element.name, item);
  }
}

printElem("", data);

答案 2 :(得分:0)

一种方法是在节点列表上使用递归: 我正在使用console.log输出结果。 多亏了VLAZ的评论。

var data = { "name": "EU", "size": 10, "nodes": [ { "name": "England", "size": 2, "nodes": [ { "name": "Center", "size": 1, "nodes": [ { "name": "main street", "size": 0.5, "nodes": [] } ] }, { "name": "Vilage", "size": 1, "nodes": [] } ] }, { "name": "Germany", "size": 4, "nodes": [] } ] }


function printValues(prefix,obj) {
  if (typeof obj !== "object") return;
  prefix = prefix?(prefix+" - "+obj.name) : obj.name;
  console.log(prefix+" "+obj.size);
  for (var key in obj.nodes) {
      printValues(prefix,obj.nodes[key]);        
  }       
}

printValues("",data);

答案 3 :(得分:0)

尝试一下-

var data = { "name": "EU", "size": 10, "nodes": [ { "name": "England", "size": 2, "nodes": [ { "name": "Center", "size": 1, "nodes": [ { "name": "main street", "size": 0.5, "nodes": [] } ] }, { "name": "Vilage", "size": 1, "nodes": [] } ] }, { "name": "Germany", "size": 4, "nodes": [] } ] }

function printValues(data, prepend) {
  console.log(prepend + data.name + ' ' + data.size);
  prepend = prepend + data.name + ' - ';
  for (node of data.nodes) {
    printValues(node, prepend)
  }
}
printValues(data, '')

答案 4 :(得分:0)

您可以对访问的名称进行封闭,并迭代节点。

const
    getItems = (parts = []) => ({ name, size, nodes = []}) => {
        console.log(`${parts.concat(name).join(' - ')} ${size}`)
        nodes.forEach(getItems(parts.concat(name)));
    };

var data = { name: "EU", size: 10, nodes: [{ name: "England", size: 2, nodes: [{ name: "Center", size: 1, nodes: [{ name: "main street", size: 0.5, nodes: [] }] }, { name: "Vilage", size: 1, nodes: [] }] }, { name: "Germany", size: 4, nodes: [] }] };

getItems()(data);