简体   繁体   中英

JavaScript: Access object variable inside nested array

How would I access ArrayObjectVariable inside ArrayObject[0] ? I know if you don't have a [ ] around it its as simple as ArrayObject[0].ArrayObjectVariable ?

var ArrayObjectVariableValue = 'AyOhVeeVee';
var ArrayObject = []
ArrayObject[0] = [{ ArrayObjectVariable : ArrayObjectVariableValue }];
alert(ArrayObject[0]???);

I didn't realize the whole "ArrayObject[0][0].ArrayObjectVariable" thing. Thanks for the replies. I was trying it with just one ("[0]") instead of two ("[0][0]"). My second question is, what is the second "[0]" for? I just tried making multiple variables and it still used "[0][0]" ? So what's the second "[0]" controlling?

Third question? I noticed that it created a variable outside the array when I did that? When I change the value of the variable in the array, it has no effect on the one outside of it? Likewise, when I change the value of the variable outside of the array it has no effect on the one inside it. Is there a way to create the array without creating a variable outside of the array with the same name? Thanks :)

OK figured it out :) Just make the Object in the array without the "[ ]". The whole point of this was to figure out how to access nested items but I got it now. Didn't realize how to make them without the "[ ]". Example for those of you struggling like I was:

    // create variables that we are going to use in Array Objects. Or make a function with the values.
    var ATV1 = 'AyTeeVeeOne', ATV2 = 'AyTeeVeeTwo', ANV1 = 'AyEnVeeOne';
    var ATV3 = 'AyTeeVeeThree', ATV4 = 'AyTeeVeeFour', ANV2 = 'AyEnVeeTwo';
    // Make an Array
    var ArrayObject;
    ArrayObject = [{}];
    // Insert variables into Array object(s).
    ArrayObject[0] = {ArrayTestObject1 : {    ArrayTestValue1:ATV1,
                                              ArrayNestedObject1:{ ArrayNestedValue1:ANV1 },
                                              ArrayTestValue2:ATV2                
                                         }};
    ArrayObject[1] = {ArrayTestObject2 : {   ArrayTestValue3:ATV3, 
                                             ArrayNestedObject2:{ ArrayNestedValue2:ANV2 },
                                             ArrayTestValue4:ATV4                 
                                         }};

    // Access Array Object Variables
    alert(ArrayObject[0].ArrayTestObject1.ArrayTestValue1) // Example 1
    alert(ArrayObject[1].ArrayTestObject2.ArrayNestedObject2.ArrayNestedValue2) // Example 2
ArrayObject[0][0].ArrayObjectVariable

您有一个数组作为ArrayObject[0]的值,因此请像对待其他数组一样对待它。

use this:here you have ArrayObject as array and you are creating index as zero to the array and in that on zeroth place ArrayObjectVariable key resides.

<script>
var ArrayObjectVariableValue = 'AyOhVeeVee';
var ArrayObject = []
ArrayObject[0] = [{
    ArrayObjectVariable : ArrayObjectVariableValue }];
    alert(ArrayObject[0][0].ArrayObjectVariable);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM