在单独的数组中以json对象的每个深度级别存储节点

时间:2022-03-23 22:43:09

i have a json, something like this

我有一个json,就像这样

{
   "a": {
      "b1": "blah blah",
      "b2": "ahsbefbasef",
      "b3": "adsfad af"
   },
   "c": {
      "d1":"asef",
      "d2":"fefef",
      "d3":"ffgfgf"
   }
}

now i want to traverse through this json and get all the nodes-depth wise, as in i want to store nodes with 0 depth(a, c) in one array, nodes with depth 1 (c1,c2,c3,d1,d2,d3) in another array, and so on and so forth, basically breaking json object and storing nodes present at each depth in seperate array

现在我想遍历这个json并获得所有节点 - 深度明智,因为我想在一个数组中存储0深度(a,c)的节点,深度为1的节点(c1,c2,c3,d1,d2) ,d3)在另一个数组中,依此类推,基本上打破json对象并以单独的数组存储每个深度处存在的节点

2 个解决方案

#1


1  

You can use below code to achieve your requirements. and in ECMAScript2015 (a.k.a. ES6), you can use the spread operator to append multiple items at once:

您可以使用以下代码来满足您的要求。在ECMAScript2015(a.k.a。ES6)中,您可以使用spread运算符一次追加多个项目:

I have benchmarked my code here , you can check below code is faster than accepted answer.

我在这里对我的代码进行了基准测试,你可以检查下面的代码比接受的答案更快。

var obj = {
  "a": {
    "b1": "blah blah",
    "b2": "ahsbefbasef",
    "b3": "adsfad af"
  },
  "c": {
    "d1": "asef",
    "d2": "fefef",
    "d3": "ffgfgf"
  }
}

var response = {};
recursive(obj, 0, response);
console.log("result:",response);


function recursive(passedObject, depth, response) {
    var keys = Object.keys(passedObject);
    response[depth] = response[depth] || [];
    response[depth].push(...keys);

    keys.forEach(function(key){
    if (passedObject[key] && typeof passedObject[key] === 'object') {
        recursive(passedObject[key], depth + 1, response)
    }
 })
}

Output:

result: 
{
 0 : ["a", "c"]
 1 : ["b1", "b2", "b3", "d1", "d2", "d3"]
}

Benchmarking results here:

基准测试结果如下:

在单独的数组中以json对象的每个深度级别存储节点

#2


4  

You could take an recursive approach by using an incremented level for each nested object. Then take the level for the result and for adding the keys of same level.

您可以通过为每个嵌套对象使用递增级别来采用递归方法。然后取结果的级别并添加相同级别的键。

function iter(object, level) {
    var keys = Object.keys(object);

    level = level || 0;
    result[level] = result[level] || [];
    Array.prototype.push.apply(result[level], keys);

    keys.forEach(function (key) {
        if (object[key] && typeof object[key] === 'object') {
            iter(object[key], level + 1);
        }
    });
}

var object = { a: { b1: "blah blah", b2: "ahsbefbasef", b3: "adsfad af" }, c: { d1: "asef", d2: "fefef", d3: "ffgfgf" } },
    result = [];

iter(object);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#1


1  

You can use below code to achieve your requirements. and in ECMAScript2015 (a.k.a. ES6), you can use the spread operator to append multiple items at once:

您可以使用以下代码来满足您的要求。在ECMAScript2015(a.k.a。ES6)中,您可以使用spread运算符一次追加多个项目:

I have benchmarked my code here , you can check below code is faster than accepted answer.

我在这里对我的代码进行了基准测试,你可以检查下面的代码比接受的答案更快。

var obj = {
  "a": {
    "b1": "blah blah",
    "b2": "ahsbefbasef",
    "b3": "adsfad af"
  },
  "c": {
    "d1": "asef",
    "d2": "fefef",
    "d3": "ffgfgf"
  }
}

var response = {};
recursive(obj, 0, response);
console.log("result:",response);


function recursive(passedObject, depth, response) {
    var keys = Object.keys(passedObject);
    response[depth] = response[depth] || [];
    response[depth].push(...keys);

    keys.forEach(function(key){
    if (passedObject[key] && typeof passedObject[key] === 'object') {
        recursive(passedObject[key], depth + 1, response)
    }
 })
}

Output:

result: 
{
 0 : ["a", "c"]
 1 : ["b1", "b2", "b3", "d1", "d2", "d3"]
}

Benchmarking results here:

基准测试结果如下:

在单独的数组中以json对象的每个深度级别存储节点

#2


4  

You could take an recursive approach by using an incremented level for each nested object. Then take the level for the result and for adding the keys of same level.

您可以通过为每个嵌套对象使用递增级别来采用递归方法。然后取结果的级别并添加相同级别的键。

function iter(object, level) {
    var keys = Object.keys(object);

    level = level || 0;
    result[level] = result[level] || [];
    Array.prototype.push.apply(result[level], keys);

    keys.forEach(function (key) {
        if (object[key] && typeof object[key] === 'object') {
            iter(object[key], level + 1);
        }
    });
}

var object = { a: { b1: "blah blah", b2: "ahsbefbasef", b3: "adsfad af" }, c: { d1: "asef", d2: "fefef", d3: "ffgfgf" } },
    result = [];

iter(object);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }