2

This is how attributes are defined. The value can be at any level deep, that is defined in element variable. Is there any way I can access deeper values?

const configurations = {
  id: "1",
  name: {
    first: "firstName",
    last: "lastName",
  },
}
const element = {
  attribute: "name.first",
}
const temp = configurations[element.attribute]; // how can I get firstName here
console.log("temp..,",temp); // now temp is undefined
Kavipriya
  • 441
  • 4
  • 17
  • There's no array, by the way, just an object. Arrays in JavaScript are denoted with the square bracket syntax `[]`. – Heretic Monkey Aug 05 '20 at 11:40
  • @HereticMonkey An object key can also be accessed using the squared bracket notation – MauriceNino Aug 05 '20 at 11:41
  • Just not with dots in it afaik. – MauriceNino Aug 05 '20 at 11:42
  • @MauriceNino Thanks, I meant the literal construction, not the accessor. – Heretic Monkey Aug 05 '20 at 11:42
  • There is no array construction going on here either though @HereticMonkey – MauriceNino Aug 05 '20 at 11:50
  • @MauriceNino That's exactly what I said in my first comment. – Heretic Monkey Aug 05 '20 at 11:53
  • I deleted my answer. But just want to clear out here. You want a dynamic solution but `configurations` is not an array its an object and you said you just want to access the firstName. – Umar Awan Aug 05 '20 at 12:06
  • I don't get what you are trying to tell us with that comment other than pointing out the obvious (There is no array - arrays use [])? But maybe that's just because of my lack of knowledge of the english language, so no offense @HereticMonkey – MauriceNino Aug 05 '20 at 12:11
  • @MauriceNino It may seem obvious to you or me, but many people have problems with the naming conventions of JavaScript when coming to it from other languages, where object-like structures might be called "associative arrays". The comment was meant for the OP, as a bit of education, given the title of the question. – Heretic Monkey Aug 05 '20 at 12:20

2 Answers2

1

Here you go:

const configurations = {
    id: "1",
    name: {
        first: "firstName",
        last: "lastName",
    },
}
const element = "name.first";


function getDeep( haystack, needle )
{
    if ( typeof needle === 'string' )
        needle  = needle.split( '.' );

    const currentNeedle = needle.shift();

    if ( typeof haystack[currentNeedle] !== 'undefined' )
    {
        if ( needle.length === 0 )
            return haystack[currentNeedle];
        else
            return getDeep( haystack[currentNeedle], needle )
    }
    else
    {
        throw new Error( 'Missing key' );
    }
}

try
{
    console.log( getDeep( configurations, element ) );
}
catch (e) {
    console.log( e );
}

Unfortunately there is no easy way to do this using a string like you wanted.

stefantigro
  • 452
  • 2
  • 12
  • Great solution. I would go one step further and use `needle + ''` to force the toString of whatever comes in the needle parameter, instead of using `typeof`. Or maybe check only for array, assuming it would be a list of properties already. – Pedro Lima Aug 05 '20 at 11:50
0

You could do it with a recursive function like so:

const configurations = {
  id: "1",
  name: {
    first: "firstName",
    last: "lastName",
  },
}
const element = {
  attribute: "name.first",
}


const getKey = (el, key) => {
  const keyParts = key.split('.');
  const currentKey = keyParts.shift();
  
  if(keyParts.length == 0) {
    return el[currentKey];
  }
  return getKey(el[currentKey], keyParts.join('.'));
}

const temp = getKey(configurations, element.attribute); // how can I get firstName here
console.log("temp..,",temp); // now temp is undefined
MauriceNino
  • 6,214
  • 1
  • 23
  • 60