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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...