I'm trying to figure out the best way to organize this data in Javascript, so I apologize if this question is broader than usual.
I have data:
State, Zip
"CA", "945"
"CA", "934"
"MA", "934"
"MA", "021"
"MA", "021"
etc.
I am trying to create some sort of Javascript Array that I can reference to get all the Zip in a certain State
This reference might look like:
State: "CA", Zip:["945", "934"]
State: "MA", Zip:["934", "021", "021"]
Does that make sense? Does something similar work with javascript?
I want to do it this way because on rare occasion a 3-digit Zip would exist in two States (as in the example above).
I was playing with the below code, but it does not work.
var stateArray = [],
var zipMapper = [];
for (var i = 0; i < data.length; i++){
if (_.contains(stateArray,data[i].state) == false) {
stateArray.push(data[i].state)
zipMapper.push({state: data[i].state, zips: []})
zipMapper[data[i].state].zips.push(data[zip])
} else {
zipMapper[data[i].state].zips.push(data[zip])
}
}
dataactually look? The pseudo representation is a bit hard to to tell.stateand onezip