I have this array :
var res_data = [
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"},
{"id": "3", "text": "CCC", "category": "drinks", "value": "3"}
];
I want to get this
{
"food": [
{
"id": "1",
"text": "AAA",
"category": "food",
"value": "1"
},
{
"id": "2",
"text": "BBB",
"category": "food",
"value": "2"
}
],
"drinks": [
{
"id": "3",
"text": "CCC",
"category": "drinks",
"value": "3"
}
]
}
I've tried to do so by iterating and set the "category" value - as a key , inside a new array, like this :
var res = [];
$.each(obj, function () {
if (this.hasOwnProperty("category")) {
res[this['category']] = this;
console.log(this['category']);
}
})
but the "food" index is keep overriding....
drinks:{id: "3", text: "CCC", category: "drinks", value: "3"}
food: {id: "3", text: "CCC", category: "food", value: "2"}
appendmean what?.The expected output is wrongdrinks=[{id: "1", text: "AAA", category: "drinks", value: "3"}] food=[{id: "2", text: "BBB", category: "food", value: "2"}, {id: "3", text: "CCC", category: "food", value: "2"}].