RoyBarOn
RoyBarOn

Reputation: 987

How to append objects to a key inside JS array?

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

Answers (5)

Narendra Jadhav
Narendra Jadhav

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

muecas
muecas

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

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

Ele
Ele

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

nem035
nem035

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

Related Questions