Davealanwint Posted April 11, 2019 Share Posted April 11, 2019 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 More sharing options...
Funce Posted April 14, 2019 Share Posted April 14, 2019 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) + Number(document.getElementById("Number2").value); My_MathSummary.push(doing_math); To access the firstNumber inside doing_math you'd access it like this. My_MathSummary[0].firstNumber; 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 More sharing options...
Davealanwint Posted April 15, 2019 Author Share Posted April 15, 2019 Implemented changes you suggested. changed to for loop instead of forEach command. The SummaryFunction is not recognizing the My_MathSummary array. new_add_app_rev_18.html Link to comment Share on other sites More sharing options...
justsomeguy Posted April 15, 2019 Share Posted April 15, 2019 It will be better if you post your code here in the forum inside a code box, you'll find that you get help quicker than asking people to download a file. Link to comment Share on other sites More sharing options...
Funce Posted April 15, 2019 Share Posted April 15, 2019 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now