I have two arrays of objects:
data = [
        {"value": 10.2, "cat": "A", "state": "MA"},
        {"value": 5.3, "cat": "B", "state": "MA"},
        {"value": 20.1, "cat": "A", "state": "NY"},
        {"value": 17.3, "cat": "B", "state": "NY"},
        {"value": 7.7, "cat": "A", "state": "CT"},
        {"value": 11.3, "cat": "B", "state": "CT"}
       ];
reference = [
             {"state": "MA", "increment": 4.1},
             {"state": "NY", "increment": 0.3},
             {"state": "CT", "increment": 20.6}
            ];
Questions
- How can I pull all - statefields from- referencewithout a loop? Specifically, a function like this- reference.pullElement("state")returns something like- ["MA", "NY", "CT"]. Does this exist? Do I have to create a- referenceobject and build the following logic into a method?- var states = []; for (var i = 0; i < reference.length; i++) { states.push(reference[i]["state"]); }
- How can I add the - incrementfield from- referenceto- valuefield in- data, so that I can have a new array like this.- output = [ {"value": 14.3, "cat": "A", "state": "MA"}, {"value": 9.4, "cat": "B", "state": "MA"}, {"value": 20.4, "cat": "A", "state": "NY"}, {"value": 17.6, "cat": "B", "state": "NY"}, {"value": 28.3, "cat": "A", "state": "CT"}, {"value": 31.9, "cat": "B", "state": "CT"} ];
I am trying to do the following for Question 2, but the actual data has a lot more categories than this. Is this the most efficient way to do it?
var output = data.slice();
for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < reference.length; j++) {
        if (data[i]["state"] == reference[j]["state"]) {
            output[i]["value"] = data[i]["value"] + reference[j]["increment"];
        }
    }
}
Thanks!
 
    