简体   繁体   English

从数组返回一个带有键的新对象

[英]return a new object with keys from array

I am trying to create a new object from the people array below, using born as a key, that contains all the people born in that year:我正在尝试从下面的people数组中创建一个新对象,使用born作为键,其中包含该年出生的所有人员:

 const newObject = {
      '1971': {
        male: [ [Object], [Object] ],
        female: [ [Object] ]
      },
      '1972': {
        male: [ [Object], [Object] ],
        female: [ [Object] ]
      }
    }

Here is my code so far:到目前为止,这是我的代码:

people.map(person => {
  let {
    born,
    sex
  } = person;

  newPeople[born] = sex == "male" ? {
    male: Array.isArray(male) && array.length ? male.push(person) : [],
    female: Array.isArray(female) && female.length ? female.push(person) : []}
})

Array of people:人群:

const people = [{
    name: 'Jim',
    sex: 'male',
    born: '1971',
    address: {
      town: 'London',
    },
  },
  {
    name: 'Bob',
    sex: 'male',
    born: '1971',
    address: {
      town: 'London',
    },
  },....

jsfiddle setup here: https://jsfiddle.net/jw9n3bmr/ jsfiddle 设置在这里: https ://jsfiddle.net/jw9n3bmr/

Here is a solution with reduce for what you want to do:以下是针对您想要执行的操作使用reduce的解决方案:

 const people = [{ name: 'Jim', sex: 'male', born: '1971', address: { town: 'London', }, }, { name: 'Bob', sex: 'male', born: '1971', address: { town: 'London', }, } ]; const result = people.reduce((acc, currVal) => { (acc[currVal.born] || (acc[currVal.born] = { male: [], female: [] }))[currVal.sex].push(currVal); return acc; }, {}); console.log(result);

I hope this could helps.我希望这会有所帮助。

const getItems = (array, key, value) => {
    let items = [];
    array.forEach((k, o) => { if (o[key] === value) items.push(o) });
    return items;
};

console.log(getItems(people, "born", "1971"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按键分组的对象数组的总和值并返回到新的数组数组中 - Sum values of array of object grouped by keys and return into a new array of arrays 从具有相同键的对象创建一个新数组 - Create a new array from object with same keys 使用新键创建新数组并从单个数组嵌套 object - Create new array with new keys and nested object from single array 我如何`.filter()`一个数组/对象并返回一个带有原始键的新数组,而不是作为一个索引数组/对象作为过滤器返回? - How do I `.filter()` an array/object and return a new array with the original keys and not as an indexed array/object as filter return? 映射数组并根据对象键返回一个新数组 - Map an array and return a new one based on the object keys 如何根据多个键从 object 数组中返回一个数组 - How to return a array from array of object based on multiple keys 使用另一个 object 的键从对象数组创建新数组 - Create new Array from array of objects with keys of another object 返回具有特定键的对象数组 - Return a array of object with sprecific keys 如何从对象中获取键和值并返回数组? - How do I get they keys and the values from the object and return an array? 我有一个 object,其中一些名称作为键,每个键都有一系列技能,我必须返回一个新的 object,其中技能作为新键,旧键作为数组 - I have an object with some names as keys, each key has an array of skills, I have to return a new object with skills as new keys and old keys as array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM