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.
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.
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.
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.
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".
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; }
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;
}, {})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With