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

3
  • 2
    Please add the code you tried so far. Commented Oct 23, 2017 at 16:29
  • 1
    This looks like you're trying to get somebody to do your work for you, which is generally not what this community is for. Sharing your previous attempts might help us debug them though. Commented Oct 23, 2017 at 16:33
  • I have edited the question, I have post the function I used. Commented Oct 23, 2017 at 16:39

2 Answers 2

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, "");

Sign up to request clarification or add additional context in comments.

3 Comments

the art of traveling is irrelevant, because you need the last point/leaf.
may i know what si the result fot .map(key => ${key}: ${obj[key]}) ??? is it ES 6 ??
@RJ That part of the code wasn't added by me (check out the revision). It just add the bills to the end of the paths.
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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.