I would like a pure JavaScript way to "Query" an Object. The Object looks as follows.
[
{
"Name": "testName1",
"Date : "01/01/2016",
"Volume1 : 1234
"Volume2 : 1234
},
{
"Name": "testName1",
"Date : "01/01/2016",
"Volume1 : 5678
"Volume2 : 5678
},
{
"Name": "testName1",
"Date : "01/02/2016",
"Volume1 : 1234
"Volume2 : 1234
},
{
"Name": "testName2",
"Date : "01/01/2016",
"Volume1 : 1234
"Volume2 : 1234
},
{
"Name": "testName2",
"Date : "01/02/2016",
"Volume1 : 1234
"Volume2 : 1234
}
]
My goal here is to be able to access the volumes of each but need to do so for specific names and dates. In other words I would like to return total Volume1 for anything with "testName1" and on the Date "01/01/2016".
I've attempted to do this by appending values to JavaScript arrays:
var dateArray =[];
var nameArray = [];
for (var i = 0; i < obj.length; i++) {
if (contains(dateArray,obj[i].date == false) { // contains is a function that checks if an item exists in an array
dateArray.push(obj[i].date;
}
}
I then do the same for names by appending unique values to a name array.
I am able to get volumes by adding:
volume += obj[i][Volume1]
in my for loop; However this doesn't distinguish across dates and names.
My thought form here was to then loop through my arrays of unique values somehow and gather values where certain criteria is met but im having trouble putting that to code.
I was also wondering if there was a much more concise way to do this.