简体   繁体   English

您能解释一下JSON对象如何存储在Javascript对象中吗?

[英]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.

相关问题 如何在其他对象中动态生成javascript对象。 可以在对象内部使用for循环吗? - How to dynamically generate javascript objects within other objects. Can you use a for loop inside an object? 你能在javascript中解释对象和数组算法吗? - Can you explain object and array arithmetic in javascript 带有内部对象的javascript json对象 - javascript json object with objects inside 您可以访问数组内的javascript自定义类对象吗?如何访问? - Can you access javascript custom class objects inside an array and how? 你能解释一下这个 JavaScript 代码是如何一步一步执行的吗? - Can you explain how this JavaScript code gets executed step by step? 我可以在JavaScript的对象中包含对象吗? - Can I have objects inside an object in JavaScript? 使用Javascript访问JSON对象内的Array内的对象 - Accessing Objects inside Array inside JSON object using Javascript 您将如何使用 Javascript 更改值/添加到对象数组内的嵌套 object 数据? - How would you change values/add to nested object data inside array of objects using Javascript? 什么是json,你能解释一下新手吗? - What is json,can you explain it to a newbie? 如何将json对象列表转换为javascript对象列表? - How do you convert a list of json objects to a list of javascript objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM