简体   繁体   English

访问JSON对象中的数组元素

[英]Accessing an array element inside JSON Object

Unintentionally, In my project I had used the following code and I was surprised to see that is working: 无意中,在我的项目中,我使用了以下代码,我很惊讶地看到它正在工作:

HTML HTML

<span id="output"></span>

Javascript 使用Javascript

var myObject = {
  'a': '----First---',
  'b': '----Second---',
  'c': '----Third----'
};

var myArray = ['a'];

// First Case
output.innerHTML = myObject[myArray];

var myArray2 = ['b'];

// Second Case
output.innerHTML += myObject[myArray2];

var myArray3 = ['a', 'b'];

// Third Case 
output.innerHTML += myObject[myArray3];

Output 产量

----First-------Second---undefined

Jsbin Link: http://jsbin.com/godilosifu/1/edit?html,js,output Jsbin Link: http ://jsbin.com/godilosifu/1/edit?html,js,output

I am directly accessing array reference within Object which should be undefined in all the cases but strangely When array is of Size 1, it always gets the first element and use that as the object key. 我直接访问Object中的数组引用,在所有情况下都应该是未定义的,但奇怪的是当数组大小为1时,它总是获取第一个元素并将其用作对象键。

I just want to know what is this concept called and why is this happening ? 我只是想知道这个概念叫什么,为什么会这样?

Because the property name has to be a string, it is type cast into one using the toString() method. 因为属性名称必须是字符串,所以使用toString()方法将其类型转换为一个字符串。 The reason that your third example is undefined is that ['a', 'b'].toString() equals 'a,b' , which is not a property in your object. 你的第三个例子未定义的原因是['a', 'b'].toString()等于'a,b' ,它不是你对象中的属性。

Property names 物业名称

Property names must be strings. 属性名称必须是字符串。 This means that non-string objects cannot be used as keys in the object. 这意味着非字符串对象不能用作对象中的键。 Any non-string object, including a number, is typecasted into a string via the toString method. 任何非字符串对象(包括数字)都通过toString方法进行类型转换为字符串。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

Worth noting is that in ECMAScript 6, there is a new collection type called Map , which allows you to use any object as a key without type coercion. 值得注意的是,在ECMAScript 6中,有一个名为Map的新集合类型,它允许您将任何对象用作没有类型强制的键。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

When you pass the array to the object as a Key, it is calling toString() on it. 当您将数组作为Key传递给对象时,它会在其上调用toString()。 This is because all Keys in Javascript are Strings. 这是因为Javascript中的所有键都是字符串。

['a'].toString() is "a"
myObject['a'] is "----First----"

See: Keys in Javascript objects can only be strings? 请参阅: Javascript对象中的键只能是字符串?

It's auto-casting. 这是自动投射。 In Javascript, only strings can be indexes into an object's properties. 在Javascript中,只有字符串可以索引到对象的属性中。 myObject is not an array, it's an object. myObject不是数组,它是一个对象。 Even though you're using the brackets to access it's properties, it is not the same meaning as brackets used on an array object. 即使您使用括号访问它的属性,它也与数组对象上使用的括号不同。

Due to object properties only being allowed to be named by strings, the compiler auto-casts your array to a string (essentially calling it's native toString() function, which for an array, joins all the elements with a comma). 由于只允许通过字符串命名对象属性,编译器会自动将您的数组转换为字符串(实质上是调用它的本机toString()函数,对于数组,它使用逗号连接所有元素)。

So when you pass your array to the object's property accessor/index, it does this: 因此,当您将数组传递给对象的属性访问器/索引时,它会执行以下操作:

myArray1 ==> "a";
myArray2 ==> "b";
myArray3 ==> "a" + "," + "b" ===> "a,b";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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