1

I have some data that is in an array that contains url's for images. Here the thing, within the img object of the data object, I want to make as many imgs as the length of the array (Make sense hopefully?). In other words, I just want to create a new object over and over again. What would you recommend as the best approach to solving this problem. I know this seems simple.

  var data = [{
                title: "asdfadf",
                thumb: "images/asdf.jpg",
                imgs: [{
                        title: "asdfasdf ",
                        thumb: "images/asdff.jpg",
                        img: "images/asdf.jpg",
                    },
                    {
                        title: "asdf",
                        thumb: "images/asdf.jpg",
                        img: "images/asdf.jpg",
                    },
                    {
                        title: "Gasdfafafasfasfasfasf. ",
                        thumb: "images/asdf.jpg",
                        img: "images/asdf.jpg",
                    },
                    {
                        title: "aswdf",
                        thumb: "images/asdfD.jpg",
                        img: "images/asdf.jpg",
                    },

                ]
            }];
2
  • 1
    This is I think is the best practice Commented Nov 7, 2013 at 18:09
  • 1
    You have extra commas all over the place Commented Nov 7, 2013 at 18:18

1 Answer 1

2

Instead of declaring data as an array, declare it as an object, while keeping the declaration of the imgs property as an array:

  var data = {
                title: "asdfadf",
                thumb: "images/asdf.jpg",
                imgs: [{
                        title: "asdfasdf ",
                        thumb: "images/asdff.jpg",
                        img: "images/asdf.jpg"
                    },
                    {
                        title: "asdf",
                        thumb: "images/asdf.jpg",
                        img: "images/asdf.jpg"
                    },
                    {
                        title: "Gasdfafafasfasfasfasf. ",
                        thumb: "images/asdf.jpg",
                        img: "images/asdf.jpg"
                    },
                    {
                        title: "aswdf",
                        thumb: "images/asdfD.jpg",
                        img: "images/asdf.jpg"
                    }

                ]
            };

Then, you could simply get the value of the imgs array property on the data object, loop over it and build up the images, for example:

var html = '';
for (var i = 0; i < data.imgs.length; i++) {

    html += 'Image title: ' + data.imgs[i].title;
    html += 'Image thumb: ' + data.imgs[i].thumb;
    html += 'Image img: ' + data.imgs[i].img + '<br>';
}

// Set the innerHtml only once, when you have built up your whole html
document.getElementById('myDiv').innerHtml = html;

As for returning an object each time, I believe that it would be much simpler if you would return the elements of the imgs array instead of creating new objects. Your imgs array already contains the objects!

for (var i = 0; i < data.imgs.length; i++) {
    // This will return each object contained within the imgs array
    console.log(data.imgs[i]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Like otherwise it's impossible? data[0].imgs[i]
I didn't say it was not possible :) I just wanted to offer him my own version. I believe that it is a lot better to declare an object, instead of declaring it as he did. Just my two cents! :)

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.