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"}
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)

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)

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)

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

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"}
]

Comments

Your Answer

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.