Reputation: 987
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"}
Upvotes: 2
Views: 76
Reputation: 10272
You can also use forEach
for array.
ES5
var arr = [{
"id": "1",
"text": "AAA",
"category": "food",
"value": "1"
},
{
"id": "2",
"text": "BBB",
"category": "food",
"value": "2"
},
{
"id": "3",
"text": "CCC",
"category": "drinks",
"value": "3"
}
],
outpt = {};
arr.forEach(function(item) {
if (!outpt.hasOwnProperty(item.category)) {
outpt[item.category] = []
}
outpt[item.category].push(item)
});
console.log(outpt)
ES6
let arr = [{
"id": "1",
"text": "AAA",
"category": "food",
"value": "1"
},
{
"id": "2",
"text": "BBB",
"category": "food",
"value": "2"
},
{
"id": "3",
"text": "CCC",
"category": "drinks",
"value": "3"
}
],
outpt = {};
arr.forEach((item) => {
if (!outpt.hasOwnProperty(item.category)) {
outpt[item.category] = []
}
outpt[item.category].push(item)
});
console.log(outpt)
Upvotes: 2
Reputation: 4335
Try this:
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"}
];
function filter(arr, key, value) {
var res = [];
for(var x = 0, max = arr.length; x < max; x = x + 1) {
if(typeof arr[x] === 'object' && arr[x].hasOwnProperty(key) && arr[x][key] === value) {
res.push(arr[x]);
}
}
return res;
}
Then you can filter by any property like this:
filter(res_data, 'category', 'food');
It will return:
[
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"}
]
Upvotes: 0
Reputation: 7933
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"}
];
var result = {};
for(var i = 0; i < res_data.length; i++){
if(result[res_data[i].category] == undefined)
result[res_data[i].category] = [];
result[res_data[i].category].push(res_data[i])
}
console.log(result)
Upvotes: 2
Reputation: 33736
Assuming a valid output, you can use the function reduce
.
var 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"}],
result = data.reduce((a, c) => {
(a[c.category] || (a[c.category] = [])).push(c);
return a;
}, {});
console.log(result);
Upvotes: 1
Reputation: 35501
A common iteration technique when converting an array into another structure is to use reduction:
const arr = [
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"},
{"id": "3", "text": "CCC", "category": "drinks", "value": "3"}
]
const result = arr.reduce((hash, item) => {
if (!hash.hasOwnProperty(item.category)) {
hash[item.category] = []
}
hash[item.category].push(item)
return hash
}, {})
console.log(result)
Upvotes: 4