Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
add[0] doesn't exist... .push something first.
add[0]
.push
add.push({ url:data.url, photo:data.thumb })
or
add[0] = {}; add[0].url = 'blah'; add[0].photo = 'foo'
Add a comment
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;
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
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.
Explore related questions
See similar questions with these tags.