Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - How to Map a list of names to a specified key using reduce

I want to learn the javascript reduce function. I have implemented the following using a different solution, but want to achieve the same through reduce.

I have an alphabets array and names array.

alphabets: ['A','B','C','D','E','F','G','H','I','J','K','L',
        'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
names: [{ name: 'Apple' },{ name: 'Apricot' },{ name: 'Avocados' },{ name: 'Almond' },
        { name: 'Blueberries' }, { name: 'Bing Cherry' },{ name: 'Breadfruit' },{ name: 'Bananas' },
        { name: 'Cherries' },{ name: 'Custard-Apple' },{ name: 'Carrot' },{ name: 'Cranberries' },
        { name: 'Dates' }, { name: 'Dragon Fruit' },{ name: 'Durian' },
        { name: 'Grapefruit' },{ name: 'Grapes' },{ name: 'Gooseberries' },{ name: 'Guava' },],

Using javascript reduce funtion, I want to map key value pair like,

A: 'Apple','Apricot','Avocados','Almond'
B: 'Blueberries', 'Bing Cherry', 'Breadfruit', 'Bananas'
C: 'Cherries', 'Custard-Apple', 'Carrot', 'Cranberries'
D: 'Dates', 'Dragon Fruit', 'Durian'

Key A should contain a list of fruits that start with A, Key B should contain a list of fruits that start with B. Kindly help me with this.

like image 943
Abhikhya Avatar asked Nov 02 '20 11:11

Abhikhya


People also ask

How do you use map and reduce together?

The function that is be passed is given arguments by the map method in the following order. For each element, the callbackfn will be passed with the value of the element as the first argument, followed by the index of the element as the second argument. Lastly the array itself as the third argument.

How do you map an array to an object?

To convert an array of objects to a Map , call the map() method on the array and on each iteration return an array containing the key and value. Then pass the array of key-value pairs to the Map() constructor to create the Map object.

What is the difference between filter map and reduce in JavaScript?

map creates a new array by transforming every element in an array individually. filter creates a new array by removing elements that don't belong. reduce , on the other hand, takes all of the elements in an array and reduces them into a single value.

What is difference between map and reduce?

Generally "map" means converting a series of inputs to an equal length series of outputs while "reduce" means converting a series of inputs into a smaller number of outputs. What people mean by "map-reduce" is usually construed to mean "transform, possibly in parallel, combine serially".


2 Answers

You could take a logical nullish assignment ??= operator fro assigning an array to a cetrain property and push the actual destrctured name.

const 
    names = [{ name: 'Apple' }, { name: 'Apricot' }, { name: 'Avocados' }, { name: 'Almond' }, { name: 'Blueberries' }, { name: 'Bing Cherry' }, { name: 'Breadfruit' }, { name: 'Bananas' }, { name: 'Cherries' }, { name: 'Custard-Apple' }, { name: 'Carrot' }, { name: 'Cranberries' }, { name: 'Dates' }, { name: 'Dragon Fruit' }, { name: 'Durian' }, { name: 'Grapefruit' }, { name: 'Grapes' }, { name: 'Gooseberries' }, { name: 'Guava' }],
    grouped = names.reduce(
        (r, { name }) => ((r[name[0].toUpperCase()] ??= []).push(name), r),
        {}
    );

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 58
Nina Scholz Avatar answered Oct 11 '22 18:10

Nina Scholz


You can just use first letter of name to be a key of value.

const groupedNames = names.reduce((acc, item) => {
  const firstLetter = item.name.charAt(0);
  if (!acc[firstLetter]) {
    acc[firstLetter] = [item.name];
    return acc;
  };
  acc[firstLetter].push(item.name);
  return acc;
}, {})
like image 39
Igor Nowosad Avatar answered Oct 11 '22 19:10

Igor Nowosad