0

I'm trying to implement a generic method to access a nested object property dynamically.

The path to the property have to be in an array of string.

So to get the label the array of string would be ['type', 'label']

I'm kinda stuck on this problem, any help ?

**Edit snipet : **

Demo

var parent = {
  type: {
    id: "2",
    label: "3",
  }
};

function getNestedLabel(ids){
if (ids.length === 1) {
  return parent[ids[0]];
}
var result = parent;
for (let i = 0; i < ids.length; i++) {
  result = result[ids[i]];
}
return result;
  }

console.log(getNestedLabel(["type", "label"]));
An-droid
  • 6,433
  • 9
  • 48
  • 93

2 Answers2

4

A simple approach would be to iterate the keyArray and also keep traversing the object with keys from keyArray

function getNestedObject( keyArr ){
    var tmp = parent; //assuming function has access to parent object here
    keyArr.forEach( function(key){
       tmp = tmp[key];
    });
    return tmp;
}

Demo

var parent = {
  type: {
    id: "2",
    label: "3",
  }
};

function getNestedObject(keyArr) {
  var tmp = parent; //assuming function has access to parent object here
  keyArr.forEach(function(key) {
    tmp = tmp[key];
  });
  return tmp;
}

console.log( getNestedObject( [ "type", "label" ] ) );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • The method is in the parent object so ok. But it has to return the string value of the nested property and not the parent itself. The caller give the path and should get the string value in return. I don't know if I'm clear in my explanation – An-droid Nov 29 '17 at 11:04
  • @An-droid Check the demo I have shared. – gurvinder372 Nov 29 '17 at 13:04
  • I tested and your solution is not working tmp is undefined at return – An-droid Nov 29 '17 at 14:01
  • @An-droid Can you share a working fiddle of your attempt? Is the demo I have shared not working for you? – gurvinder372 Nov 29 '17 at 14:02
  • "run code snipet" is not working indeed. I integrated your method as it is with just 'this' instead of 'parent'. And as i'm using it in ngfor it's really bad for performances. I've updated my question with my current attempt – An-droid Nov 29 '17 at 14:21
  • I updated a snipet, it kinda works but as I said it's not usable in my case. Used in ngfor it makes the UI lags as hell :/ I would need to save each nested properties to process it only one time and juste return it the other times .. – An-droid Nov 29 '17 at 14:46
  • @An-droid I don't see why UI will lag because of this. – gurvinder372 Nov 29 '17 at 14:48
  • I don't either, but anyway this is another issue. You helped and your answer is not bad so I'll accept it thank you :) – An-droid Nov 29 '17 at 15:03
  • More compact with `reduce`: `const getNestedObject = (parent, keyArr) => keyArr.reduce((currentObject, property) => currentObject?.[property], parent); getNestedObject({ type: { id: "2", label: "3" } }, [ "type", "label" ]);`. – Sebastian Simon Apr 09 '21 at 01:17
0

what about this:

getNestedObject(parent: any, id:string[]): string{

    let child = {...parent};
    let result: string;
    for(let i=0; i<id.length; i++){
        if( i !== id.length-1 ){
            child = child[id[i]]
        }
        else{
            result = child[id[i]]
        }
    }
    return result;
}
GHB
  • 917
  • 6
  • 13