且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

引用嵌套的JavaScript对象

更新时间:2023-01-17 16:22:03

让我们来看看每个例子都有什么问题,然后采取看看哪种方式正常。



示例1



object ['object1'] ['string'] ['nameString']


  1. 我们希望 object ['object1'] 返回对象 string ,对吧?因此,让我们通过替换它的一部分来简化大表达式。这将使我们更容易理解。


  2. 所以现在我们有 string ['string'] ['nameString']


  3. 但是字符串没有名为'string'的成员,所以 string [ 'string'] 返回 undefined


  4. 当您尝试将 undefined 视为对象时,会出现错误!


示例2



object.object1.string.nameString


  1. 我们期待 object.object1 返回对象 string ,对吗?因此,让我们通过替换它的一部分来简化大表达式。这将使我们更容易理解。


  2. 所以现在我们有 string.string.nameString


  3. 但是 string 没有名为'string'的成员,所以 string。 string 返回 undefined


  4. 当您尝试将 undefined 视为对象时,会出现错误!。


你想要什么



object.object1.nameString (或 object ['object1'] ['nameString']


  1. 我们希望 object.object1 返回对象 string ,对吗?因此,让我们通过替换它的一部分来简化大表达式。这将使我们更容易理解。


  2. 所以现在我们有 string.nameString ,我们希望返回nameValue


  3. 确实如此!



I have this code:

var string = { 
            nameString : "nameValue",
            nameString2 : "nameValue2",
            nameString3 : "nameValue3",
            datathing : 0,
        };

var data = { 
            data : 1,
            dataNum2 : 2,
            dataNum3 : 3,
            dataNum4 : 4,
        };

var thing = { 
            datathing1 : 10,
            datathing2 : 20,
            datathing3 : 30,
            datathing4 : 40,
        };

var object = { 
            object1 : string,
            data1 : data,
            thing1 : thing,
        };

Why do neither of these means to access the data work:

alert("testReference= " + object['object1']['string']['nameString']);
alert("testReference= " + object.object1.string.nameString);

I cannot understand it, even though similar examples found below and textbooks state explicitly that they should work:

Accessing nested JavaScript objects with string key

Thanks in advance for any input!

I am currently constructing an object and passing it around, a 'for in' will bring up the values but a 'typeof' test or any other way I try and access will not work, either I will encounter an error (which breaks the program, I think) or I get 'undefined'....

One last thing if this gets solved, is it ok to nest a key that is the same name value as its parent, such as data.data - this leads to the possibility of further nesting such as data.data.data...

Let's look at what's wrong with each example, then take a look at the way that works right.

Example 1

object['object1']['string']['nameString']

  1. We expect object['object1'] to return the object string, right? So lets simplify the big expression by replacing that part of it. That'll make it easier for us to understand.

  2. So now we have string['string']['nameString'].

  3. But string has no member called 'string', so string['string'] returns undefined.

  4. And when you try to treat undefined as an object, you get an error!

Example 2

object.object1.string.nameString

  1. We expect object.object1 returns the object string, right? So lets simplify the big expression by replacing that part of it. That'll make it easier for us to understand.

  2. So now we have string.string.nameString.

  3. But string has no member called 'string', so string.string returns undefined.

  4. And when you try to treat undefined as an object, you get an error!.

What You Want

object.object1.nameString (or object['object1']['nameString'])

  1. We expect object.object1 returns the object string, right? So lets simplify the big expression by replacing that part of it. That'll make it easier for us to understand.

  2. So now we have string.nameString, and we expect that to return "nameValue".

  3. And it does!