Jump to content

creating an array of objects

Recommended Posts

I have four items in an object and want to store the result in an array. I want to add objects to the array and access the individual items in an object within the array. Isobject_in_array_project.htmlobject_in_array_project.htmlobject_in_array_project.html this possible. some literature says yes and other says no. Basicly a database in memory is what I am trying to do.

Link to post
Share on other sites

Accessing an Object is different from accessing an Array. Accessing an Object inside an Array follows the standard conventions.

Objects are indexed via keyNames, and can be accessed via dot-notation (object.property) or bracket-notation (object["property"])

Arrays are 0-indexed ascending in position order and can only be accessed via bracket-notation (array[0])

Using your example where the code is

doing_math.firstNumber = Number(document.getElementById("Number1").value);
doing_math.secondNumber = Number(document.getElementById("Number2").value);
doing_math.answer = Number(document.getElementById("Answer").value);
doing_math.equals =
  Number(document.getElementById("Number1").value) +

To access the firstNumber inside doing_math you'd access it like this.


My_MathSummary is an array, and the doing_math 'object' is in the first position, so we access it by getting [0] (the first index).
Then we treat My_MathSummary[0] like its a 'doing_math' object (which it is) and just get firstNumber. If you added additional doing_math objects, you would need to use a for loop to iterate through My_MathSummary

Link to post
Share on other sites


Here's the button for the code blocks.


My_MathSummary can't be recognised because you're not passing it to your SummaryFunction.

function SummaryFunction(add_to, demo, My_MathSummary)
<button onclick="SummaryFunction(add_to, My_MathSummary)">Show summary</button>

There's a bit of a discrepancy there in how many parameters you've got.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...