I have been looking for a few days now and I haven't found the answer to this specific problem. I'm receiving a javascript object array from an endpoint in my API. I need to group the objects together based on the Type.
example hard-coded array of objects:
$scope.content = {
things: [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
}
var typeArr = [];
for (var key in $scope.content.things) {
typeArr[key] = $scope.content.things[key].thingType;
}
typeArr = _.uniq(typeArr);
typeArr is now going to be [load, export] What I need to happen next is to compare all the objects in things[ ] such that
if(typeArr[key] === things[i].thingType)
would push that object like so:
typeArr = [
load: {
thing: 'one',
thingType: 'load'
},
export: [{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
]
in other words, I need the objects to remain whole, and I need to categorize them, and nest them according to a shared type. I have seriously been stuck on this all week. Any insight would be greatly appreciated.