1

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.

LCaraway
  • 1,257
  • 3
  • 20
  • 48
  • Possible duplicate of [How can I only keep items of an array that match a certain condition?](http://stackoverflow.com/questions/27131984/how-can-i-only-keep-items-of-an-array-that-match-a-certain-condition) – Scimonster Jan 11 '16 at 16:23

1 Answers1

2

First off your javascript and json is a mess. Lots of missing Quotes and mismatched parentheses.

var arr = [ /*your array*/ ];
var total = arr
  //filters so only items with .name === "testName1" remain
  .filter(function(x) { return x.name === "testName1"; })
  //filters so only items with .date === "01/01/2016" remain
  //this could be replaced with a more robust date check
  .filter(function(x) { return x.date === "01/01/2016"; })
  //sums up all of the values of Volume1 of the remaining items.
  .reduce(function(prev, curr, index) { return prev + curr.Volume1;});

Each one of these function calls is basically doing a for loop to perform a particular action based on the predicate you give it (predicate being a function that returns a boolean value).

for instance filter() could be replaced with a for loop that looks like this.

var output = [];
for(var x in array) {
  if(predicate(array[x])) {
    output.push(array[x]);
  }
}
return output;

When you are working with for loops sometimes its a good idea to break down your idea into single tasks for each loop. It is less efficient (in execution) but it can help you break down your idea into manageable chunks. Then once you really understand your code you can try merging them back into a single loop.

Jordan Davidson
  • 419
  • 4
  • 14
  • This did the trick, I used sort of a hybrid of what you suggested and what i had in mind. Works perfectly. Was unaware of the .filter method. Because the idea is that the json will change everyday, i first looped threw to find out unique dates. Then loop through those dates while applying .filter to calculate needed volumes and append those values to a separate array. I can now match up the date with volume by using the same index on either array. I may try to combine the two into one key:value array. Thanks again, this really helped me think through the code. – LCaraway Jan 11 '16 at 18:36