简体   繁体   中英

reduce/filter array of objects by object key

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!

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;
}, {});

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM