Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append objects to a key inside JS array?

Tags:

javascript

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"}
like image 789
RoyBarOn Avatar asked Jun 21 '26 02:06

RoyBarOn


2 Answers

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)
like image 148
nem035 Avatar answered Jun 22 '26 14:06

nem035


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)
like image 38
Narendra Jadhav Avatar answered Jun 22 '26 16:06

Narendra Jadhav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!