3

Good Day People

I have an array of objects and I need to print out the path of each node value and last print key and value for special (by name) node.

This is the array of objects or JSON

[{
    "Name": "2007",
    "Elements": [{
            "Name": "country1",
            "Elements": [{
                "House": "house1",
                "water": 1.8
            }],
            "Data": {}
        },
        {
            "Name": "country2",
            "Elements": [{
                "Name": "city2",
                "Elements": [{
                    "Name": "neighbourhood2",
                    "Elements": [{
                        "House": "house2",
                        "water": 2.8
                    }]
                }],
                "Data": {}

            }],
            "Data": {}
        },
        {
            "Name": "country3",
            "Elements": [{
                "House": "house2",
                "uni bill": 3.8
            }],
            "Data": {}
        }
    ],
    "Data": {}
}]

The output should be like this

2007 > country1 > house > water: 1.8
2007 > city2 > neighbourhood2 > house2 > electricity: 2.8
2007 > country3 > house > uni bill: 3.8

++++++++++++++ edited +++++++++++++++

function objectToPaths(data) {
    var validId = /^[a-z_$][a-z0-9_$]*$/i;
    var result = [];
   doIt(data, "");
    return result;

    function doIt(data, s) {
      if (data && typeof data === "object") {
       if (Array.isArray(data)) {
          for (var i = 0; i < data.length; i++) {
            doIt(data[i], s + "");
          }
        } else {
          for (var p in data) {
            if (validId.test(p)) {

              doIt(data[p], s + " > " + data[p]);
             } else {
              doIt(data[p], s + "");
           }
          }
        }
      } else {
        result.push(s);
      }
   }
 }

this is a rewrite of a function I found here but I did not get the expected result

+++++++++++++++++++++++ end of the edit +++++++++++++++++++++++

Please help

Thanks in advance

rebh. a.m
  • 97
  • 1
  • 5

2 Answers2

4

What you are looking for is a Depth First Traversal function that recursively print properties:

function print(arr, path) {                              // print takes an array an an accumulated path from which it will start printing
  arr.forEach(function(obj) {                            // for each object obj in the array
    if(obj.Elements) {                                   // if the object obj has sub elements in it
      print(obj.Elements, path + " > " + obj.Name);      // then call print on those elements, providin the absolute path to this object
    } else {                                             // otherwise (it is a leaf)
      const bills = Object.keys(obj)
        .filter(key => key !== "House")
        .map(key => `${key}: ${obj[key]}`)
        .join(', ')
      console.log(path.slice(3) + " > " + obj.House + " > " + bills);    // print the accumulated path along with the House property of this object (removing the first 3 letters from path which are equal to " > ")
    }
  });
};

var arr = [{"Name":"2007","Elements":[{"Name":"country1","Elements":[{"House":"house1","water":1.8}],"Data":{}},{"Name":"country2","Elements":[{"Name":"city2","Elements":[{"Name":"neighbourhood2","Elements":[{"House":"house2","water":2.8}]}],"Data":{}}],"Data":{}},{"Name":"country3","Elements":[{"House":"house2","uni bill":3.8}],"Data":{}}],"Data":{}}];

print(arr, "");
shawon191
  • 1,945
  • 13
  • 28
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • the art of traveling is irrelevant, because you need the last point/leaf. – Nina Scholz Oct 23 '17 at 17:22
  • may i know what si the result fot .map(key => `${key}: ${obj[key]}`) ??? is it ES 6 ?? – RJ- Oct 23 '17 at 20:10
  • @RJ That part of the code wasn't added by me (check out the [revision](https://stackoverflow.com/posts/46894743/revisions)). It just add the bills to the end of the paths. – ibrahim mahrir Oct 30 '17 at 16:06
2

You could take a function for iterating and collect the path to the last object.

function iter(array, path) {
    path = path || [];
    array.forEach(function (o) {
        if (o.Elements) {
            return iter(o.Elements, path.concat(o.Name));
        }
        Object.keys(o).forEach(function (k) {
            if (k !== 'House') {
                console.log(path.concat(o.House, k).join(' > ') + ': ' + o[k]);
            }
        });            
    });
}

var data = [{ Name: "2007", Elements: [{ Name: "country1", Elements: [{ House: "house1", water: 1.8 }], Data: {} }, { Name: "country2", Elements: [{ Name: "city2", Elements: [{ Name: "neighbourhood2", Elements: [{ House: "house2", water: 2.8 }] }], Data: {} }], Data: {} }, { Name: "country3", Elements: [{ House: "house2", "uni bill": 3.8 }], Data: {} }], Data: {} }];

iter(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392