javascript - Filtering an array of objects by an object property within an object within the array -
i'm in nested hell of objects, here. feel there's answer this, can't quite figure out how phrase what's happening.
so, giant array of objects rest return (something 450+ objects) comes loaded this:
results[0] = { prop0: "string", prop1: "string", prop2: bool, prop3: object { prop0: "string" } prop4: "string" }
i'm doing thing, first, i'm creating array of objects contains unique strings out of nested object's property, along count of number of times string appears within data set.
i need, each unique string, create array of objects within original dataset results.prop3.prop0 matches string in collapsed array. confusing enough?
from this:
name: "joe",
place: state {
state: "new york",
},
alive: false
}
to this:
new york (120)
joe - dead
i've gotten far, using underscore.js:
for(index in collapsed){ var details = _.where(results,{state: {state: collapsed[index][0]}}); console.log(details); }
but i'm idiot somewhere, because that's returning empty array each thing in collapsed array.
sample of collapsed
[{"new york",120},{"georgia",79},{"another place",15}].
i want build table of data where, underneath each place, load in objects place matches state string way @ bottom of nested object.
to jm_____'s point, i'm going try that.
here examples of data manipulation underscore based on _.reduce()
, _.filter()
getting collapsed array number of items grouped states
var collapsed = _.reduce(response, function(memo, item) { if ( ! memo[item.place.state]) { memo[item.place.state] = 0; } memo[item.place.state]++; return memo; }, {});
result: grouped object states keys , count values.
{ london: 2, new york: 4 }
getting filtered data state
var filtered = _.filter(response, function(item) { return item.place.state === "new york"; });
result: array of objects state new york
link jsbin demo
Comments
Post a Comment