Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a JavaScript function to lookup objects and arrays nested within each other

I have an object with nested arrays and object that looks like this:

// Recursive nested objects & arrays:
var obj = {
  children: [{
      parentname: "level 0, Benny",
      children: [{
          parentname: "level 0.0, Stevey",
          children: [{
              parentname: "level 0.0.0, Betty",
              children: []
            },
            {
              parentname: "level 0.0.1, AFGNCAAP",
              children: [{
                parentname: "level 0.0.1.0, Moonman",
                children: []
              }]
            }
          ]
        },
        {
          parentname: "level 0.1, Oreo",
          children: []
        }
      ]
    },
    {
      parentname: "level 1, Jupiter",
      children: []
    }
  ]
};

// I am able to retrieve values with various length formulas:
console.log(obj.children[0].parentname); // level 0, Benny
console.log(obj.children[0].children[0].parentname); // level 0.0, Stevey
console.log(obj.children[0].children[0].children[0].parentname); // level 0.0.0, Betty
console.log(obj.children[0].children[0].children[1].parentname); // level 0.0.1, AFGNCAAP
console.log(obj.children[0].children[0].children[1].children[0].parentname); // level 0.0.1.0, Moonman
console.log(obj.children[0].children[1].parentname); // level 0.1, Oreo
console.log(obj.children[1].parentname); // level 1, Jupiter


// And I able to reduce formulas into functions...per level of nesting, example 1 level:
function getNameWorking1(name, level) {
  console.log(obj[name][level].parentname);
}
getNameWorking1(["children"], [1]) // level 1, Jupiter

// And example 2 levels:
function getNameWorking2(name, level) {
  console.log(obj[name][level[0]][name][level[1]].parentname);
}
getNameWorking2(["children"], [
  [0],
  [1]
]) // level 0.1, Oreo

// I am trying to create a single function that I can send a parameter with the sequence of property name & array values.  This does not work:
function getNameNotWorking(parameter) {
  console.log(obj[parameter].parentname);
}
getNameNotWorking("[children][1]");
getNameNotWorking("[children][0][children][1]");

I am trying to create a single function that I can send a parameter with the sequence of property name & array values. This last function is not working. Any help on how to do this?

BTW. I have a recursive function that does work for this. But it works by every time starting at the beginning and look reading through each nested object & array until the item I am looking for is found. This seems it may be excessive to do repetitively. So I am looking for a method to look up the object once I already know it location - via all the children array values.

like image 444
Adam Stacko Avatar asked May 30 '26 20:05

Adam Stacko


1 Answers

I feel you should pass your parameters separated by white sapace instead of swrapping in square brackets to make parsing and solution easier.

And. here you go :)

function getNameWorking1(parameter, obj) {
  paramsList = parameter.split(' ').filter(prop => Boolean(prop)).map(prop => prop.trim());

  return paramsList.reduce((val, prop) => {
    return val[prop];
  }, obj).parentname;
}


//Working example:

var obj = {
  children: [{
      parentname: "level 0, Benny",
      children: [{
          parentname: "level 0.0, Stevey",
          children: [{
              parentname: "level 0.0.0, Betty",
              children: []
            },
            {
              parentname: "level 0.0.1, AFGNCAAP",
              children: [{
                parentname: "level 0.0.1.0, Moonman",
                children: []
              }]
            }
          ]
        },
        {
          parentname: "level 0.1, Oreo",
          children: []
        }
      ]
    },
    {
      parentname: "level 1, Jupiter",
      children: []
    }
  ]
};

console.log(getNameWorking1("children 1", obj));
console.log(getNameWorking1("children 0 children 1", obj));
like image 185
Gaurav Saraswat Avatar answered Jun 02 '26 11:06

Gaurav Saraswat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!