[英]Can you explain how JSON objects are stored inside a Javascript object?
Here I'm iterating over JSON objects stored inside a javascript object and print some of it's values. 在这里,我遍历存储在javascript对象中的JSON对象,并打印其中的一些值。
database.ref().once("value", function (snap) {
//store javascript object containing JSON objects in scores variable.
scores = snap.val();
for(var key in scores){
console.log(scores[key].Name);
}
});
scores object look like this. 分数对象看起来像这样。
How does the for loop retrieve the key in each JSON object and store in the variable named "key"? for循环如何在每个JSON对象中检索密钥并将其存储在名为“ key”的变量中?
Since scores is not an array but a javascript object how does a for loop work at all? 由于scores不是数组而是javascript对象,因此for循环如何工作?
For example scores[1].Name
won't work. 例如,
scores[1].Name
。名称无效。 So, it's not index based. 因此,它不是基于索引的。
In your example you have an object that looks like: 在您的示例中,您有一个类似于以下内容的对象:
var obj = {
'key1': {name: 'Josh', score: 9},
'key2': {name: 'July', score: 30},
'key3': {name: 'Joy', score: 18},
'key4': {name: 'Barbara', score: 50},
};
Doing for (var key in obj) {...}
will loop through all the keys of your initial object. 做
for (var key in obj) {...}
将循环遍历初始对象的所有键 。 In that example, that would be ['key1', 'key2', 'key3', 'key4']
. 在该示例中,该
['key1', 'key2', 'key3', 'key4']
。
You are right to say that only array items can be access by index. 您说对了,只能按索引访问数组项。 For objects, children are accessed by keys (which are strings).
对于对象,可以通过键(是字符串)访问子级。
Both the following work: 以下两项工作:
// Directly when you know the key name
console.log(obj.key1.name); // Will print out 'Josh'
// Using a variable when the key name is stored in a variable
var customKey = 'key1';
console.log(obj[customKey].name); // Will print out 'Josh'
In your initial example, you are using the for ... in
structure to loop through he object keys and then accessing the nested objects by key. 在第一个示例中,您使用
for ... in
结构遍历对象键,然后通过键访问嵌套对象。
You can verify what I am saying with the following example: 您可以通过以下示例验证我的意思:
database.ref().once("value", function (snap) {
//store javascript object containing JSON objects in scores variable.
scores = snap.val();
for(var key in scores){
console.log(key); // See what's inside your key variable
console.log(scores[key].Name);
}
});
The for...in
statement iterates over the enumerable properties of the given object. for...in
语句遍历给定对象的可枚举属性 。
Asuming you have the following object: 假设您具有以下对象:
var o = {
a: 1,
b: 2,
c: {
d: 3
}
};
Doing: 这样做:
for (var key in o) console.log(key, o[key]);
Will print: 将打印:
a 1
b 2
c { d: 3 }
I think it is important to keep in mind that the for...in
statement also loops through the inherited properties of the object. 我认为重要的是要记住,
for...in
语句还会循环遍历对象的继承属性 。
Doing: 这样做:
o.__proto__.x = 4;
for (var key in o) console.log(key, o[key]);
Will print: 将打印:
a 1
b 2
c { d: 3 }
x 4
If you want to loop only through the properties that belong to the object directly, and not to the prototype chain, you can use Object.keys()
: 如果只想直接遍历该对象的属性,而不是原型链,则可以使用
Object.keys()
:
var o = {
a: 1,
b: 2,
c: function () {}
};
o.__proto__.x = 4;
o.__proto__.y = function () {};
var keys = Object.keys(o);
for (var i = 0; i < keys.length; i++) console.log(keys[i], o[keys[i]]);
The code above prints: 上面的代码打印:
a 1
b 2
c function () {} //functions are also enumerable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.