1

I'm trying to return a new array of objects from the nested properties of another object. This is my current object:

const teams = {
  GRYFFINDOR: {
    display: 'Gryffindor',
    channel: ['#team-gryffindor'],
    ui: 'lion',
  },
  HUFFLEPLUFF: {
    display: 'Hufflepuff',
    channel: ['#team-hufflepuff'],
    ui: '', // empty string on purpose
  },
  SLYTHERIN: {
    display: 'Slytherin',
    channel: ['#team-slytherin'],
    ui: 'snake',
  },
}

Now, I'm trying to return an array of objects like so:

[
  { value: 'lion' },
  { value: '' },
  { value: 'snake' },
]

This is what I have tried:

const teamsUI = [Object.keys(teams).reduce((carry, item) => {
  return {...carry, ...{ ['value']: teams[item].ui  }}
}, {})];;

However, I get this:

console.log(teamsUI); // returns [ { value: 'snake' }]

What am I doing wrong?

1
  • 1
    [...Object.values(<obj>)].map(v => ({value: v.ui})) or alike should do Commented Mar 10, 2021 at 19:16

3 Answers 3

1

You could do like this:

const teams = {
  GRYFFINDOR: {
    display: 'Gryffindor',
    channel: ['#team-gryffindor'],
    ui: 'lion',
  },
  HUFFLEPLUFF: {
    display: 'Hufflepuff',
    channel: ['#team-hufflepuff'],
    ui: '', // empty string on purpose
  },
  SLYTHERIN: {
    display: 'Slytherin',
    channel: ['#team-slytherin'],
    ui: 'snake',
  },
}

const result = Object.values(teams).map(({ ui }) => ({ value: ui }));

console.log(result);

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

Comments

1

Beside nested result structure, you could take the values and destructure the wanted property formapping.

const
    teams = { GRYFFINDOR: { display: 'Gryffindor', channel: ['#team-gryffindor'], ui: 'lion' }, HUFFLEPLUFF: { display: 'Hufflepuff', channel: ['#team-hufflepuff'], ui: '' }, SLYTHERIN: { display: 'Slytherin', channel: ['#team-slytherin'], ui: 'snake' } },
    result = Object.values(teams).map(({ ui: value }) => ({ value }));
    
console.log(result);

Comments

1

Here is the shortest way to achieve what you want

Object.keys(teams).map(t=>({value: teams[t].ui}))

Now what are you doing wrong?

Take a look at this part

Object.keys(teams).reduce((carry, item) => {
  return {...carry, ...{ ['value']: teams[item].ui  }}
}, {});

You're reducing your object to a single value with the same key called value. This means that the value of teams[item].ui of the current object in the reduce function will override the previous and hence you'll end up with the last object of that reduce function. You're finally wrapping this last object in an array which gives you what you have.

A better way to achieve that will be to do something like this

Object.keys(teams).reduce((carry, item, index)=>{
  carry[index] = { value: teams[item].ui };
  return carry;
}, []);

Comments

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.