简体   繁体   English

您将如何使用 Javascript 更改值/添加到对象数组内的嵌套 object 数据?

[英]How would you change values/add to nested object data inside array of objects using Javascript?

const beers = [
  {
    id: '100',
    name: 'stoneys'
  },
  {
    id: '200',
    name: 'budweiser'
  },

  {
    id: '300',
    name: 'miller'
  },

  {
    id: '400',
    name: 'corona'
  }
];

const people = [
  {
    name: 'steve',
    teams: [
      {
        name: 'pirates',
        beers: ['100']
      },
      {
        name: 'penguins',
        beers: ['300']
      }
    ]
  },
  {
    name: 'jim',
    teams: [
      {
        name: 'indians',
        beers: ['200']
      },
      {
        name: 'blue jackets',
        beers: ['100', '400']
      }
    ]
  }
];

let newPeople = people.map(fan => {
  fan.teams.map(team => {
    team.beers.map(beer => beers.filter(brand => brand.id === beer)[0])
  });
});

Above is a sample I put together to best demonstrate my question.以上是我整理的一个示例,以最好地展示我的问题。 I am having trouble understanding why nested mapping (.map()) of object arrays is not allowing me to alter the nested data.我无法理解为什么 object arrays 的嵌套映射 (.map()) 不允许我更改嵌套数据。 When I console log results, I am either getting an "[undefined, undefined]' or the unchanged "people" array.当我控制台日志结果时,我得到一个“[未定义,未定义]”或未更改的“人”数组。

I would like to return the same array as "people" except replace the nested "beers" array (people.teams.beers[]) with corresponding objects from the "beers" array.我想返回与“人”相同的数组,除了用“啤酒”数组中的相应对象替换嵌套的“啤酒”数组(people.teams.beers [])。 Example of a successful result below:下面的成功结果示例:

 {
    name: 'steve',
    teams: [
      {
        name: 'pirates',
        beers: [
          {
            id: '100',
            name: 'stoneys'
          }
        ]
      },
      {
        name: 'penguins',
        beers: [
          {
            id: '300',
            name: 'miller'
          }
        ]
      }
    ]
  }

Array.map expects a function which takes single array element as parameter and returns a mapped value. Array.map需要一个 function ,它将单个数组元素作为参数并返回一个映射值。 In your case you're not returning any value from mapping functions therefore you're getting undefined twice在您的情况下,您没有从映射函数返回任何值,因此您两次未定义

 const beers = [ { id: '100', name: 'stoneys' }, { id: '200', name: 'budweiser' }, { id: '300', name: 'miller' }, { id: '400', name: 'corona' } ]; const people = [ { name: 'steve', teams: [ { name: 'pirates', beers: ['100'] }, { name: 'penguins', beers: ['300'] } ] }, { name: 'jim', teams: [ { name: 'indians', beers: ['200'] }, { name: 'blue jackets', beers: ['100', '400'] } ] } ]; let newPeople = people.map(fan => { let teams = fan.teams.map(team => { let beer = team.beers.map(beer => beers.filter(brand => brand.id === beer)[0]); return { name: team.name, beers: beer } }); return { name: fan.name, teams: teams } }); console.log(newPeople);

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

相关问题 想要在嵌套对象内添加对象数组:Javascript - Want to add an array of objects inside a nested object: Javascript 您将如何使用猫鼬填充嵌套在数组对象中的引用? - How would you populate references nested in an object of an array using mongoose? 如何使用javascript将object of object个数组对象改成数组对象 - How to change object of object of array objects to array objects using javascript 如何更改嵌套对象数组 javascript 中的所有键名 - How to change all key name inside nested array of objects javascript 如何从嵌套在数组 JAVASCRIPT 内的对象中提取值 - how to extract values from an objects nested inside array JAVASCRIPT 如何使用JavaScript在对象数组中添加对象 - How to add object in an array of objects using Javascript 如何使用 javascript 对数组内的嵌套对象数组执行操作? - How to perform operations on nested array of objects inside an array using javascript? 缩进 object 嵌套对象数组中的值 JavaScript - Indent object values in an array of nested objects in JavaScript 如何更改嵌套 object 中的值以与数组连接,并将 id 与 javascript 匹配 - How to change values in nested object to concat with array, and matching with id with javascript 如何将新对象添加到嵌套在 ReactJS 对象数组中的对象数组? - How to add a new object to an array of objects that is nested inside an array of objects in ReactJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM