1
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. 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. Example of a successful result below:

 {
    name: 'steve',
    teams: [
      {
        name: 'pirates',
        beers: [
          {
            id: '100',
            name: 'stoneys'
          }
        ]
      },
      {
        name: 'penguins',
        beers: [
          {
            id: '300',
            name: 'miller'
          }
        ]
      }
    ]
  }
3
  • Can you post the code you have tried? Commented Apr 14, 2020 at 23:11
  • map returns the mapped result ... you don't do anything with it Commented Apr 14, 2020 at 23:11
  • @terrymorse - OP's code is in the first code block at the bottom Commented Apr 14, 2020 at 23:12

1 Answer 1

1

Array.map expects a function which takes single array element as parameter and returns a mapped value. 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);

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

2 Comments

This works perfectly for me! Thank you! Now, is there a way to make the returned objects dynamic so I don't have to individually manage the key-value pairings in each returned object if I add or remove a field from the original object array? I tried using Object.assign(), however, I could not successfully return the correct objects.
Can you open another question to avoid editing this one?

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.