1

I want to do a grouping for JavaScript object as age.My JSON is

var jsonObj=[{"name":"john","age":23},{"name":"mark","age":25},{"name":"jeni","age":21}]`

I want to be the group result like here.

[23:{"name":"john","age":23},25:{"name":"mark","age":25},21:{"name":"jeni","age":21}]

Please help me to get a result, I try with map and filter but did not get the result.

4
  • 2
    what do you do with two objects with same age? Commented Nov 23, 2017 at 17:32
  • 1
    poll is gone in the process :( Commented Nov 23, 2017 at 17:33
  • @NinaScholz yes i fogot that it should come as array. Commented Nov 23, 2017 at 17:40
  • @Kaddath sorry for wrong input, see the edit. Commented Nov 23, 2017 at 17:43

4 Answers 4

6

Use Array#reduce to group the objects. Because there can multiple people with the same age, collect them into an array under the age property:

var jsonObj=[{"name":"john","age":23},{"name":"mark","age":25},{"name":"poll","age":23},{"name":"jeni","age":21}];

var result = jsonObj.reduce(function(r, o) {
  r[o.age] || (r[o.age] = []); // if the age key doesn't exist, set it to be an array
  
  r[o.age].push(o); // push the object into the array

  return r;
}, {});

console.log(result);

Or the fancy ES6 one liner version:

var jsonObj=[{"name":"john","age":23},{"name":"mark","age":25},{"name":"poll","age":23},{"name":"jeni","age":21}];

var result = jsonObj.reduce((r, o) => ((r[o.age] || (r[o.age] = [])).push(o), r), {});

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

3

You could take the hash table as result. Then loop and create new arrays for new keys. Later push the object.

var data = [{ name: "john", age: 23 }, { name: "mark", age: 25 }, { name: "poll", age: 23 }, { name: "jeni", age: 21 }],
    result = {};

data.forEach(o => (result[o.age] = result[o.age] || []).push(o));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

You can use underscore js to solve this problem.
Add underscore js first

<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js' ></script>
then you can simply get the result.

var result=_.indexBy(jsonObj, 'age');

3 Comments

How can you handle with the object with two same age?
then you can use _.groupBy(jsonObj, 'age')
Yes. It is a simple way with underscore js. thankyou
0

You can try it :

var jsonObj = [{ "name": "john", "age": 23 }, { "name": "mark", "age": 25 }, { "name": "poll", "age": 23 }, { "name": "jeni", "age": 21 }];
let result = {};
jsonObj.forEach(item => {
    if(result.hasOwnProperty(item.age)) {
        let arr = [];
        if(Array.isArray(result[item.age])) {
            result[item.age].push(item);
        } else {
            result[item.age] = [result[item.age], item];
        }
    } else {
        result[item.age] = item;
    }
});
console.log(result)

2 Comments

in your jsonObj two object has same ages it should come as array
ou , I understand you

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.