1
var add = [];
add[0].url = data.url;
add[0].photo = data.thumb;
console.log(add);

This'll be an instant know for most JS people. The code above is failing, anyone able to tell me where I'm going wrong

Ivo Wetzel
46.8k17 gold badges102 silver badges112 bronze badges
asked Nov 22, 2010 at 1:56

3 Answers 3

6

add[0] doesn't exist... .push something first.

add.push({ url:data.url, photo:data.thumb })

or

add[0] = {};
add[0].url = 'blah';
add[0].photo = 'foo'
answered Nov 22, 2010 at 1:58
Sign up to request clarification or add additional context in comments.

Comments

4

You are assigning to properties of the add[0] object but you haven't made any such object.

add[0] = { };
add[0].url = data.url;
add[0].photo = data.thumb;
answered Nov 22, 2010 at 1:59

Comments

3

A different option for you; instead of pushing into an empty array, and declaring an empty object

var add = new Array(); //create new array object
add[0] = {url:data.url, photo:data.thumb}; //add new object with items url and photo
answered Nov 22, 2010 at 2:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.