2

Consider the following array of objects

const data = [{
    name: 'James',
    sex: 'male',
  }, {
    name: 'Mike',
    sex: 'male',
  }, {
    name: 'Janet',
    sex: 'female',
  }, {
    name: 'Mary',
    sex: 'female',
}];

I'd like to return the following:

{
  males: [{
    name: 'James',
    sex: 'male',
  }, {
    name: 'Mike',
    sex: 'male',
  }]
  females: [{
    name: 'Janet',
    sex: 'female',
  }, {
    name: 'Mary',
    sex: 'female',
}]
}

I've tried mapping over the array, selecting objects by key then adding them to a new array, the result being two arrays. From there, I don't know how to go about returning them in a new object in a succinct way.

I'm not too familiar with the array filter/reduce methods in ES6, would they be useful here? Any help is appreciated!

5 Answers 5

2

You can use reduce

const data = [{
  name: 'James',
  sex: 'male',
}, {
  name: 'Mike',
  sex: 'male',
}, {
  name: 'Janet',
  sex: 'female',
}, {
  name: 'Mary',
  sex: 'female',
}];

const result = data.reduce((c, v) => {
  c[v.sex] = c[v.sex] || [];       //Initiate if key does not exist
  c[v.sex].push(v);                //Push the value
  return c;
}, {});

console.log(result);

If you want the keys to be plural, you can just append 's'

const result = data.reduce((c, v) => {
  let k = v.sex + 's';
  c[k] = c[k] || [];           //Initiate if key does not exist
  c[k].push(v);                //Push the value
  return c;
}, {});
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help :)
2

Something simple with plain old js -

const data = [{
    name: 'James',
    sex: 'male',
  }, {
    name: 'Mike',
    sex: 'male',
  }, {
    name: 'Janet',
    sex: 'female',
  }, {
    name: 'Mary',
    sex: 'female',
}];

var result = { males: [], females: []};
data.forEach(function(person){
  person.sex === 'male' ? result.males.push(person) : result.females.push(person);
});

console.log(result);

1 Comment

Thank you, love the simplicity
1

Try following

const data = [{name:'James',sex:'male',},{name:'Mike',sex:'male',},{name:'Janet',sex:'female',},{name:'Mary',sex:'female',}];

var result = data.reduce((acc, obj) => {
  acc[obj.sex + "s"] = acc[obj.sex + "s"] || []; 
  acc[obj.sex + "s"].push(obj);
  return acc;
}, {});
console.log(result);

Comments

1

You can do:

const data = [{name: 'James',sex: 'male'}, {name: 'Mike',sex: 'male'}, {name: 'Janet',sex: 'female'}, {name: 'Mary',sex: 'female'}];
const result = data.reduce((a, c) => (a[c.sex + 's'].push(c), a), {males: [], females: []});

console.log(result);

Comments

1

Did you answer your question?

const data = [{
    name: 'James',
    sex: 'male',
  }, {
    name: 'Mike',
    sex: 'male',
  }, {
    name: 'Janet',
    sex: 'female',
  }, {
    name: 'Mary',
    sex: 'female',
}];
const sex = {};
data.forEach(item => {
    if (!sex[item.sex]) sex[item.sex] = [];
    sex[item.sex].push(item);
})
console.log(sex);

1 Comment

Yes, thank you - great solution, though I've picked the one using reduce above :)

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.