For 循环不适用于 json 数据
For loop is not working with json data
我正在尝试使用以下代码在 javascript 中创建一个循环。它从 json 数据中获取长度。
console.log(albums.data.length);
行正在运行并返回 3. 为什么循环不起作用?
console.log(x);
不返回任何内容,甚至不返回空行。
控制台也没有错误。
function getBestPhoto(albums){
console.log(albums);
console.log(albums.data.length);
for(var x in albums.data.length){
console.log(x);
for(var y in albums.data[x].photos.length){
console.log(y);
}
}
}
我尝试了另一种类型的循环(for(var i = 0; i < blabla; i++)
),但它也不起作用。
编辑:
我想用
for(var x = 0; x < albums.data.length; x++){
console.log(albums.data[x].photos.id);
}
而不是
for(var x in albums.data){
我该怎么做?
您应该从循环中删除 .length
function getBestPhoto(albums){
console.log(albums);
console.log(albums.data.length);
for(var i = 0; i < albums.data.length; i++){
var x = albums.data[i];
console.log(x);
for(var j = 0; j < albums.data[i].photos.length; j++){
var y = albums.data[i].photos[j];
console.log(y);
console.log(albums.data[i].photos[j].id);
}
}
}
for-in 循环不适用于数组,它用于遍历对象的 properties/fields。如果 albums.data
是数组,则应改用 forEach
循环语句。如果 albums.data
是一个对象,而您正试图访问其 properties/fields/属性,则可以使用 for-in
结构。
如果albums.data
是一个数组,试试:
albums.data.forEach(function(element, index) {
// Here you have access to each element (object) of the "albums.data" array
console.log(element.toString());
// You can also access each element's properties/fields if the element is an
// object.
// If the element is an array itself, you need to iterate over its elements
// in another inner foreach.
// Here we are accessing the "photos" property of each element - which itself
// is another array.
element.photos.forEach(function(photo, index) {
// Here you have access to the elements of the "photos" array
// Each element of the "photos" array is put in the photo variable.
// Assuming each element of the "photos" array is an object, you can access
// its properties, using the dot notation:
console.log("photo id=", photo.id);
// If the property name (e.g. "id") is not a valid javascript name
// (has some special symbols), use the bracket notation
console.log("photo URL=", photo["photo-url"]);
});
});
您也可以为此使用 lodash 库(以及许多其他简洁的功能)。
如果 albums.data
是一个对象,尝试:
for (var prop in albums.data) {
// Again, this construct is for accessing properties of an object
// (e.g. if albums.data is an object), not an array.
console.log("property name=" + prop + ", " + "property value=" +
albums.data[prop]);
}
我正在尝试使用以下代码在 javascript 中创建一个循环。它从 json 数据中获取长度。
console.log(albums.data.length);
行正在运行并返回 3. 为什么循环不起作用?
console.log(x);
不返回任何内容,甚至不返回空行。
控制台也没有错误。
function getBestPhoto(albums){
console.log(albums);
console.log(albums.data.length);
for(var x in albums.data.length){
console.log(x);
for(var y in albums.data[x].photos.length){
console.log(y);
}
}
}
我尝试了另一种类型的循环(for(var i = 0; i < blabla; i++)
),但它也不起作用。
编辑:
我想用
for(var x = 0; x < albums.data.length; x++){
console.log(albums.data[x].photos.id);
}
而不是
for(var x in albums.data){
我该怎么做?
您应该从循环中删除 .length
function getBestPhoto(albums){
console.log(albums);
console.log(albums.data.length);
for(var i = 0; i < albums.data.length; i++){
var x = albums.data[i];
console.log(x);
for(var j = 0; j < albums.data[i].photos.length; j++){
var y = albums.data[i].photos[j];
console.log(y);
console.log(albums.data[i].photos[j].id);
}
}
}
for-in 循环不适用于数组,它用于遍历对象的 properties/fields。如果 albums.data
是数组,则应改用 forEach
循环语句。如果 albums.data
是一个对象,而您正试图访问其 properties/fields/属性,则可以使用 for-in
结构。
如果albums.data
是一个数组,试试:
albums.data.forEach(function(element, index) {
// Here you have access to each element (object) of the "albums.data" array
console.log(element.toString());
// You can also access each element's properties/fields if the element is an
// object.
// If the element is an array itself, you need to iterate over its elements
// in another inner foreach.
// Here we are accessing the "photos" property of each element - which itself
// is another array.
element.photos.forEach(function(photo, index) {
// Here you have access to the elements of the "photos" array
// Each element of the "photos" array is put in the photo variable.
// Assuming each element of the "photos" array is an object, you can access
// its properties, using the dot notation:
console.log("photo id=", photo.id);
// If the property name (e.g. "id") is not a valid javascript name
// (has some special symbols), use the bracket notation
console.log("photo URL=", photo["photo-url"]);
});
});
您也可以为此使用 lodash 库(以及许多其他简洁的功能)。
如果 albums.data
是一个对象,尝试:
for (var prop in albums.data) {
// Again, this construct is for accessing properties of an object
// (e.g. if albums.data is an object), not an array.
console.log("property name=" + prop + ", " + "property value=" +
albums.data[prop]);
}