0

I am having a problem which I think I might have figured out before how to do it but I can't remember now and can't figure it out.

Let's say we have an object thats a few levels deep, meaning it has as values other objects which also have as some of the values objects and so on.

Now how could I make a function to which I pass the object and and adress inside it and I can access the value at that location inside the function like this:

const getValueAtAdress = (object, 'country.city.rules') => {
return //here I need to return the value at object.country.city.rules.
}

Am I missing something obvious?

Talmacel Marian Silviu
  • 1,596
  • 2
  • 6
  • 14
  • 2
    This answer has been generally already answered on stackoverflow. That being said the most simplifest form and without taking into account if there are arrays or special cases would be something like this; `path.split('.').reduce((acc, cur) => acc[cur], obj)` – kemicofa ghost Sep 11 '20 at 21:13
  • you could iteratively navigate by splitting the "address" on `.`, and use the brackets notation. Basically, you want `object["country"]["city"]["rules"]` (with checks for `undefined`, probabyly – Pac0 Sep 11 '20 at 21:14

2 Answers2

0

Your code shows a function declaration but you can't declare an argument name in quotes

You can however call a function and pass a string.

In that case, you just need to split the string into an array and then loop over that array, building up a "chained" set of string indexes that can be passed to the object. The String.split() and Array.reduce() methods are the key.

let obj = {
  county: {
      city: {
        rules: "Strict"
      }
  }
};

const getValueAtAddress = (object, countyCityRules) => { 
  // Split the string at the dots to form an array...
  // The loop over that array and reduce it with an
  // accumulator that is then applied to the object.
  return countyCityRules.split(".").reduce((acc, cur) => acc[cur], obj);;
}

console.log(getValueAtAddress(obj, "county"));
console.log(getValueAtAddress(obj, "county.city"));
console.log(getValueAtAddress(obj, "county.city.rules"));
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • this does not answer the original question...I need to be able to pass a string and the string can have multiple elements and the function will not know in advance how many elements – Talmacel Marian Silviu Sep 11 '20 at 21:25
  • @TalmacelMarianSilviu Sorry, your question does not show you *calling* the function and passing a string. It shows you *declaring* a function and incorrectly declaring an argument, so that wasn't clear. The answer though is the same - - passing strings as indexes to the object. I'll update the answer to show that. – Scott Marcus Sep 11 '20 at 21:31
  • thanks but I already specified that you won't knoe that the value you need to get is exactly 3 elements deep. it could be 5 or it could be 7, so that wouldn't really work – Talmacel Marian Silviu Sep 11 '20 at 21:34
  • @TalmacelMarianSilviu You keep saying that this solution won't work, but it will. It's just a matter of looping over the array after you split the string. I'll update again, but this ***is*** the answer. – Scott Marcus Sep 11 '20 at 23:26
0

I thought I'd mention here for posterity that what helped me was the answer using the reduce which is exactly what I used before but I could not remember:

Example that I am using for my particular problem:

let stateLocation = address.split('.').reduce((acc, cur) => acc[cur], state);
Talmacel Marian Silviu
  • 1,596
  • 2
  • 6
  • 14