Javascript 如何读取/访问名称:数组中存储的对象的值?
如何访问randomImage.images数组中存储的每个对象的名称:值 我尝试使用for in循环(参考randomImage.imagePicker方法),但得到的输出是:Javascript 如何读取/访问名称:数组中存储的对象的值?,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,如何访问randomImage.images数组中存储的每个对象的名称:值 我尝试使用for in循环(参考randomImage.imagePicker方法),但得到的输出是: 0[object Object] 1[object Object] 2[object Object] 3[object Object] 不要在循环中使用for…来迭代数组 您可以使用object.keys获取对象的可枚举自身属性 randomImage.images.forEach(function(obj) {
0[object Object]
1[object Object]
2[object Object]
3[object Object]
不要在循环中使用
for…来迭代数组
您可以使用object.keys
获取对象的可枚举自身属性
randomImage.images.forEach(function(obj) {
var prop = Object.keys(obj)[0],
value = obj[prop];
console.log(prop, value);
});
不要在
循环中使用for…来迭代数组
您可以使用object.keys
获取对象的可枚举自身属性
randomImage.images.forEach(function(obj) {
var prop = Object.keys(obj)[0],
value = obj[prop];
console.log(prop, value);
});
不要在
循环中使用for…来迭代数组
您可以使用object.keys
获取对象的可枚举自身属性
randomImage.images.forEach(function(obj) {
var prop = Object.keys(obj)[0],
value = obj[prop];
console.log(prop, value);
});
不要在
循环中使用for…来迭代数组
您可以使用object.keys
获取对象的可枚举自身属性
randomImage.images.forEach(function(obj) {
var prop = Object.keys(obj)[0],
value = obj[prop];
console.log(prop, value);
});
使用console.log(k,this.images[k]);(逗号而非加号)
您将得到如下输出:
0 Object {image 1: "http://placehold.it/100x100"}
1 Object {image 2: "http://placehold.it/200x200"}
2 Object {image 3: "http://placehold.it/300x300"}
3 Object {image 4: "http://placehold.it/400x400"}
使用console.log(k,this.images[k]);(逗号而非加号)
您将得到如下输出:
0 Object {image 1: "http://placehold.it/100x100"}
1 Object {image 2: "http://placehold.it/200x200"}
2 Object {image 3: "http://placehold.it/300x300"}
3 Object {image 4: "http://placehold.it/400x400"}
使用console.log(k,this.images[k]);(逗号而非加号)
您将得到如下输出:
0 Object {image 1: "http://placehold.it/100x100"}
1 Object {image 2: "http://placehold.it/200x200"}
2 Object {image 3: "http://placehold.it/300x300"}
3 Object {image 4: "http://placehold.it/400x400"}
使用console.log(k,this.images[k]);(逗号而非加号)
您将得到如下输出:
0 Object {image 1: "http://placehold.it/100x100"}
1 Object {image 2: "http://placehold.it/200x200"}
2 Object {image 3: "http://placehold.it/300x300"}
3 Object {image 4: "http://placehold.it/400x400"}
一种解决方案是:
(函数(){
var随机图像={
图像:[
{'image 1':'http://placehold.it/100x100'},
{‘图2’:'http://placehold.it/200x200'},
{‘图3’:'http://placehold.it/300x300'},
{‘图4’:'http://placehold.it/400x400'}
],//图像数组
imagePicker:函数(){
for(此.images中的var k){
var curImg=this.images[k];
var key=Object.keys(curImg)[0];
log(k+this.images[k]);
msg=k+':'+key+'=>'+curImg[key];
document.getElementById('el').innerHTML+=''+msg+'';
}
}//图像选择器法
}//随机图像对象
randomImage.imagePicker()
})()
一种解决方案如下:
(函数(){
var随机图像={
图像:[
{'image 1':'http://placehold.it/100x100'},
{‘图2’:'http://placehold.it/200x200'},
{‘图3’:'http://placehold.it/300x300'},
{‘图4’:'http://placehold.it/400x400'}
],//图像数组
imagePicker:函数(){
for(此.images中的var k){
var curImg=this.images[k];
var key=Object.keys(curImg)[0];
log(k+this.images[k]);
msg=k+':'+key+'=>'+curImg[key];
document.getElementById('el').innerHTML+=''+msg+'';
}
}//图像选择器法
}//随机图像对象
randomImage.imagePicker()
})()
一种解决方案如下:
(函数(){
var随机图像={
图像:[
{'image 1':'http://placehold.it/100x100'},
{‘图2’:'http://placehold.it/200x200'},
{‘图3’:'http://placehold.it/300x300'},
{‘图4’:'http://placehold.it/400x400'}
],//图像数组
imagePicker:函数(){
for(此.images中的var k){
var curImg=this.images[k];
var key=Object.keys(curImg)[0];
log(k+this.images[k]);
msg=k+':'+key+'=>'+curImg[key];
document.getElementById('el').innerHTML+=''+msg+'';
}
}//图像选择器法
}//随机图像对象
randomImage.imagePicker()
})()
一种解决方案如下:
(函数(){
var随机图像={
图像:[
{'image 1':'http://placehold.it/100x100'},
{‘图2’:'http://placehold.it/200x200'},
{‘图3’:'http://placehold.it/300x300'},
{‘图4’:'http://placehold.it/400x400'}
],//图像数组
imagePicker:函数(){
for(此.images中的var k){
var curImg=this.images[k];
var key=Object.keys(curImg)[0];
log(k+this.images[k]);
msg=k+':'+key+'=>'+curImg[key];
document.getElementById('el').innerHTML+=''+msg+'';
}
}//图像选择器法
}//随机图像对象
randomImage.imagePicker()
})()
您可以使用forEach
循环访问值
this.images.forEach(function(image){
for(var key in image) {
console.log('key', key);
console.log('value', image[key]);
}
});
您可以使用forEach
循环访问这些值
this.images.forEach(function(image){
for(var key in image) {
console.log('key', key);
console.log('value', image[key]);
}
});
您可以使用forEach
循环访问这些值
this.images.forEach(function(image){
for(var key in image) {
console.log('key', key);
console.log('value', image[key]);
}
});
您可以使用forEach
循环访问这些值
this.images.forEach(function(image){
for(var key in image) {
console.log('key', key);
console.log('value', image[key]);
}
});
还有一个选择。对于现在的结构,需要两个循环。一个循环遍历图像数组,另一个循环访问密钥
imagePicker:函数(){
对于(var i=0,len=this.images.length;i还有一个选项。对于现在的结构,您需要两个循环。一个循环遍历图像数组,另一个循环访问键
imagePicker:函数