1

I have a JavaScript object, that is multiple levels deep, for example:

let obj = [
    {
        "testKeyOne": "one",
        "testKeyTwo": "two"
    },
    {
        "testKeyThree": "three",
        "testKeyFour": "four",
        "testKeyFive": {
            "testKeyFiveAndAHalf": "5.5"
            "testKeyFiveAndThreeQuarters": "5.75"
        }
    },
]

I also have an array for the key of what I need to access, for example, if I'm looking for the 5.5 one,

let array = [1, "testKeyFive", "testKeyFiveAndAHalf"]

though my array may also look like this if I'm looking for "one"

let array = [0, "testKeyOne"]

Is there any way to use the array to access the desired value?

This is my first time asking a question so if I messed up or if there is anything unclear or something that needs to be changed I apologize.

Thank you!

isherwood
  • 58,414
  • 16
  • 114
  • 157
will
  • 137
  • 3
  • 9

4 Answers4

5

Yep. You can just use a reduce on the array:

let result = array.reduce((value, entry) => value[entry], obj);
IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
  • should probably use `(value, entry) => value && value[entry]` to return `undefined` when an invalid path is specified - your program would throw an error. – Mulan Aug 19 '19 at 20:38
  • Do you know if there would be a way to edit the value that you get? – will Aug 19 '19 at 20:38
  • @user633183 It should probably have some check for it, if there's no guarantee of valid paths, yes. – IceMetalPunk Aug 19 '19 at 20:40
  • @nywillb Yes? It just returns the value from the object, you can work with it after the same way you would if you accessed it directly. – IceMetalPunk Aug 19 '19 at 20:41
  • @IceMetalPunk sorry, I meant edit it directly in the object, so the original object is changed? – will Aug 19 '19 at 20:43
  • @nywillb like this? [Set nested item in object/array from array of keys](https://stackoverflow.com/questions/52458222) – adiga Aug 19 '19 at 20:44
  • @nywillb Objects in JS are references to the original. So if you just target the object one layer above, you can set values on it. – IceMetalPunk Aug 19 '19 at 20:44
  • @adiga yep that exactly! Thanks! – will Aug 19 '19 at 20:45
1
let desired = obj; 
while(array.length > 0) { desired = desired[array[0]]; array.shift() }
console.log(desired)

this should work

Dominik Matis
  • 2,086
  • 10
  • 15
0

Here's one way to do it:

let obj = [{
    "testKeyOne": "one",
    "testKeyTwo": "two"
  },
  {
    "testKeyThree": "three",
    "testKeyFour": "four",
    "testKeyFive": {
      "testKeyFiveAndAHalf": "5.5",
      "testKeyFiveAndThreeQuarters": "5.75"
    }
  },
]

let arr = [
  [1, "testKeyFive", "testKeyFiveAndAHalf"],
  [0, "testKeyOne"]
]

function foo(objArr, accessArr) {
  for (const [index, ...keys] of accessArr) {
    let obj = objArr[index];
    for (const key of keys) {
       obj = obj[key];
    }
    console.log(obj)
  }
}

foo(obj, arr);
connexo
  • 53,704
  • 14
  • 91
  • 128
0

You can use a recursive function like that

let obj = [{
    testKeyOne: "one",
    testKeyTwo: "two"
  },
  {
    testKeyThree: "three",
    testKeyFour: "four",
    testKeyFive: {
      testKeyFiveAndAHalf: "5.5",
      testKeyFiveAndThreeQuarters: "5.75"
    }
  }
];

let array = [1, "testKeyFive", "testKeyFiveAndAHalf"];

function getValue(arr, obj) {
  const [first, ...rest] = arr;
  return typeof(obj[first]) === "object" ? getValue(rest, obj[first]) : obj[first];
}

console.log(getValue(array, obj));
Aziz.G
  • 3,599
  • 2
  • 17
  • 35