0

How do I access the name:values of each Object stored within the randomImage.images Array?

I tried using a for in loop (see the randomImage.imagePicker Method for reference) but the output I get is:

0[object Object]
1[object Object]
2[object Object]
3[object Object]

(function() {

var randomImage = {

        images : [
                { 'image 1' : 'http://placehold.it/100x100'},
                { 'image 2' : 'http://placehold.it/200x200'},
                { 'image 3' : 'http://placehold.it/300x300'},
                { 'image 4' : 'http://placehold.it/400x400'}
                ],//images Array
        imagePicker : function () {
                        for (var k in this.images ) {
                            console.log(k+this.images[k]);
                        }
                }//imagePicker Method

}//randomImage Object

randomImage.imagePicker()

})()
2
  • try making a global reference to images and inspecting it with Google Chrome Developer tools. You can add a watch under Source and see the complete structure of the object. That will tell you if what you want is available or not. Commented Oct 12, 2015 at 16:55
  • 1
    Are the objects really needed? Why don't you store the image urls directly in the array? images: [ "http://...", "http://...", ...] Commented Oct 12, 2015 at 16:59

6 Answers 6

1

One solution is like that :

(function() {

var randomImage = {

        images : [
                { 'image 1' : 'http://placehold.it/100x100'},
                { 'image 2' : 'http://placehold.it/200x200'},
                { 'image 3' : 'http://placehold.it/300x300'},
                { 'image 4' : 'http://placehold.it/400x400'}
                ],//images Array
        imagePicker : function () {
                        for (var k in this.images ) {
                            
                            var curImg = this.images[k];
                            var key = Object.keys(curImg)[0];
                            
                          console.log(k+this.images[k]);

                          msg = k + ' : ' + key + ' => ' + curImg[key];
                          document.getElementById('el').innerHTML+='<div>'+msg+'<div>';
                        }
                }//imagePicker Method

}//randomImage Object

randomImage.imagePicker()

})()
<div id ='el'><div>

Sign up to request clarification or add additional context in comments.

Comments

1

Don't use for...in loops to iterate arrays.

You can get the enumerable own properties of an object with Object.keys.

randomImage.images.forEach(function(obj) {
  var prop = Object.keys(obj)[0],
      value = obj[prop];
  console.log(prop, value);
});

Comments

1

You can access the values using a forEach loop.

this.images.forEach(function(image){
    for(var key in image) {
        console.log('key', key);
        console.log('value', image[key]);
    }
});

Comments

0

use console.log(k,this.images[k]); ( Comma and not plus sign)

and you will get output as :

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"}

1 Comment

OP wants to access the key/value directly, not as an object.
0

One more option. For the structure you have now you need two loops. One to loop through the images array and another loop to access the keys.

imagePicker : function () {

    for ( var i=0, len=this.images.length; i<len; i++ ) {

        var imgObj = this.images[ i ];

        for ( var key in imgObj ) {
            console.log( key + imgObj[ key ] );
        }

    }
}

Though I wonder if you could get away with just using an array to store your image URLs.

images: [
    'http://placehold.it/100x100',
    'http://placehold.it/200x200',
    'http://placehold.it/300x300',
    'http://placehold.it/400x400'
    ]

Comments

0
function() {
  var i = 0;
  var len = this.images.length;
  //loop through the images
  for (i; i < len; i++) {
    // access the Image object
    var oImage = this.images[i];
    // get the keys of the Image object
    var aKeys = Object.keys(oImage);
    // log the url of the image by accessing the first key
    console.log(oImage[aKeys[0]]);
  }
}

A better solution would be to design a better data model like:

var images = [
 {
   name: 'image 1',
   url: '//placehold.it/100x100'
 }
 ...
]

And access the Image name and url like:

function() {
  var i = 0;
  var len = this.images.length;

  for (i; i < len; i++) {
    var oImage = this.images[i];
    console.log(oImage.url, oImage.name);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.