After searching online, I couldn't find the answer I was looking for but I'm confident it's out there. Please direct me to it if this question has been addressed before. Apologies for any duplicate posts.
Hello, I am attempting to retrieve the values of a specific key within an array of arrays. My objective is to calculate the sum of a "column."
Here is an example:
Below is my array of arrays
var total = 0;
var dataset= [
{"cost" : 5.00 , "name" : "Victor" },
{"cost" : 6.00 , "name" : "Jack" },
{"cost" : 7.00 , "name" : "Bill" },
{"cost" : 8.00 , "name" : "Delilah" },
{"cost" : 9.00 , "name" : "Robert" },
{"cost" : 10.00 , "name" : "Marisa" }
]
Is there a way to get the total sum of all cost values in one iteration? Or can the cost values be displayed as a single array at once?
If I gather all the cost values, it should appear like this:
[5.00,6.00,7.00,8.00,9.00,10.00]
If there is a predefined function that I am unaware of, then I would expect something like this:
total = dataset["cost"].sum(); <-- pseudo code
console.log(total) //would print 45
I prefer not to iterate through each index of my dataset.
Thank you in advance