0

So why I can't access the array property by array[0].obj.accessible ?

var array = [{
    option1 : '',
    option2 : '',
    option3 : '',

    TEST : {
        accessible : '',
        optional : ''
    },
    TEST2 : {
        accessible : '',
        optional : ''
    },
    TEST3 : {
        accessible : '',
        optional : ''
    }
}];

function updateArray(obj, acc, opt) {
    // this is not working - why?
    //array[0].obj.accessible = acc;
    //array[0].obj.optional = opt;

    // this is working fine:
    array[0].option1 = 'option1';
    // or this:
    array[0].TEST.accessible = acc;

    // so why array[0].obj is not refer to 'TEST' ? (obj = 'TEST')
    // cause I don't want to build the function updateArray X times with array[0].TEST.accessible, array[0].TEST2.accessible, array[0].TEST3.accessible, etc.
}

updateArray('TEST', 'yes', 'true');

console.log(array);
Abslen Char
  • 3,071
  • 3
  • 15
  • 29
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – James Apr 14 '18 at 19:04
  • Try `array[0][obj].accessible = acc;`, and reading the link above. – Takit Isy Apr 14 '18 at 19:07
  • Don't quite understand the downvotes. This is a valid question and one that probably tripped all of us up when first learning javascript :) – Alex Apr 14 '18 at 19:09
  • Have a look at this and scroll down to „nested objects“. https://www.w3schools.com/js/js_json_objects.asp — try to assemble your object using quotes. –  Apr 14 '18 at 19:09
  • Ohh thanks I tried with array[0]->.<-[obj] - the dot shouldn't be there. My mistake, thanks for link. – glotfiovic1v Apr 14 '18 at 19:10

3 Answers3

0
// this is not working - why?
//array[0].obj.accessible = acc;
//array[0].obj.optional = opt;

Because there is no key named obj in your object at 0 element of the array.

var array = [{
    obj : {
        accessible : '',
        optional : ''
    },

    ...
}];

In the above, I created key obj which is an object with some other keys. Now this will work:

array[0].obj.accessible = acc;
array[0].obj.optional = opt;

At the second look at your function, I think you want to be able to dynamically access a property of an object in your function. If so use [] and pass a name of a key:

function updateArray(obj, acc, opt) { // obj === 'TEST'
    array[0][obj].accessible = acc;
    array[0][obj].optional = opt;
}
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
0

You should use:

array[0][obj].accessible

Using array[0].obj is looking for key obj inside array[0] but there is no such key.

ashfaq.p
  • 5,379
  • 21
  • 35
0

What a great question! This is indeed a tricky thing to get used to when you are learning JavaScript!

In order to access object properties dynamically, one must use the bracket syntax ([]) not the dot . syntax.

Like so:

function updateArray(obj, acc, opt) {
  array[0][obj].accessible = acc;
  array[0][obj].optional = opt;
  ...
}
Alex
  • 64,178
  • 48
  • 151
  • 180