0

I have been trying to summarize a list of two entries objects in a single one using the array's reduce method, but what I have in return is the value of one of the last entry. The code:

const swatches = [
           { emoji: "lion"    , color: "#ff691f" },
           { emoji: "tiger"   , color: "#fab81e" },
           { emoji: "fish"    , color: "#7fdbb6" },
           { emoji: "frog"    , color: "#19cf86" },
           { emoji: "pig"     , color: "#f58ea8" },
           { emoji: "unicorn" , color: "#981ceb" },
           { emoji: "rabbit"  , color: "#ffffff" },
           { emoji: "wolf"    , color: "#000000" },
           ]

const animals = swatches.reduce( (emojis, entry) => emojis[entry.emoji] = entry.color, {} )

3 Answers 3

2

You need to return the accumulator emojis.

const swatches = [
           { emoji: "lion"    , color: "#ff691f" },
           { emoji: "tiger"   , color: "#fab81e" },
           { emoji: "fish"    , color: "#7fdbb6" },
           { emoji: "frog"    , color: "#19cf86" },
           { emoji: "pig"     , color: "#f58ea8" },
           { emoji: "unicorn" , color: "#981ceb" },
           { emoji: "rabbit"  , color: "#ffffff" },
           { emoji: "wolf"    , color: "#000000" },
           ]

const animals = swatches.reduce(
    (emojis, entry) => (emojis[entry.emoji] = entry.color, emojis),
    {}
);

console.log(animals);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

To fix your code return the accumulator object (emojis) at the end of the reducer function:

const swatches = [{"emoji":"lion","color":"#ff691f"},{"emoji":"tiger","color":"#fab81e"},{"emoji":"fish","color":"#7fdbb6"},{"emoji":"frog","color":"#19cf86"},{"emoji":"pig","color":"#f58ea8"},{"emoji":"unicorn","color":"#981ceb"},{"emoji":"rabbit","color":"#ffffff"},{"emoji":"wolf","color":"#000000"}]

const animals = swatches.reduce( (emojis, entry) => {
  emojis[entry.emoji] = entry.color
  return emojis
}, {})

console.log(animals)

You can also map the array to an array of entries - [key, value], and then convert it to an object with Object.fromEntries():

const swatches = [{"emoji":"lion","color":"#ff691f"},{"emoji":"tiger","color":"#fab81e"},{"emoji":"fish","color":"#7fdbb6"},{"emoji":"frog","color":"#19cf86"},{"emoji":"pig","color":"#f58ea8"},{"emoji":"unicorn","color":"#981ceb"},{"emoji":"rabbit","color":"#ffffff"},{"emoji":"wolf","color":"#000000"}]

const animals = Object.fromEntries(swatches.map(({ emoji, color }) => [emoji, color]))

console.log(animals)

2 Comments

...or simply Object.fromEntries(swatches.map(Object.values))
Although we've got object keys traversal order in ES6, it's still a bit risking (numeric keys for example), so I prefer the more explicit form.
0

You would need to return the ("new") accumulator (your emojis object) from the callback, to be used in the next iteration or as the final result:

const animals = swatches.reduce((emojis, entry) => {
    emojis[entry.emoji] = entry.color;
    return emojis;
}, {});

It might be simpler to work with a normal loop:

const animals = {};
for (const entry of swatches)
    animals[entry.emoji] = entry.color;

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.