1
var testArray = [];

var testImage = new Image();
testImage.src = "entities/left.png";
testArray.push({image: testImage, xpos: 100, ypos: 100, width: 100, height: 100});

console.log(testArray.image);

The output from the console.log says "undefined", if I log testArray[0] it says "image: img" in the image part. is there a way to push the actual img element into the array?

2
  • You already did it. To access image object use console.log(testArray[0].image); Commented Apr 27, 2020 at 21:44
  • 1
    You are pushing the image in testArray[0].image Commented Apr 27, 2020 at 21:45

2 Answers 2

1

You're already accessing that element, through testArray[0].image, and that's the only way to access element within an array (index-based). There's not dot notation (e.g. testArray.image) for accessing array's entries.

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

1 Comment

Happy to be helpful. Remember to mark the question as answered.
0

You have created an array of object, which is correct. Try this testArray[0].image

This says that you are accessing image property of your first element of array

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.