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

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'
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;

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

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.