2

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"}
Narendra Jadhav
10.3k15 gold badges35 silver badges45 bronze badges
asked Mar 21, 2018 at 17:54
4
  • append mean what?.The expected output is wrong Commented Mar 21, 2018 at 17:57
  • I think your expected output should be drinks=[{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"}]. Commented Mar 21, 2018 at 17:58
  • Hey, thanks, i want to map the object by categroy, and to create a new object with the correct values... Commented Mar 21, 2018 at 18:00
  • @NarendraJadhav - YEAH , that's what i'm looking for Commented Mar 21, 2018 at 18:01

5 Answers 5

4

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)

answered Mar 21, 2018 at 17:58
Sign up to request clarification or add additional context in comments.

Comments

2

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)

answered Mar 21, 2018 at 18:04

Comments

2

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)

answered Mar 21, 2018 at 18:01

Comments

1

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);

answered Mar 21, 2018 at 18:01

Comments

0

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"}
]
answered Mar 21, 2018 at 18:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.