roberto-butti / some-drops-of-javascript

This book collects some useful, unknown, underrated JavaScript functions discovered and learned while using JavaScript daily.
https://drops-of-javascript.hi-folks.dev/
Other
137 stars 40 forks source link

Added property description for reduce() #5 #25

Closed mahesha1150 closed 11 months ago

mahesha1150 commented 11 months ago

HI @roberto-butti, Please review the pull request and let me know if any changes have to be made.

vercel[bot] commented 11 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
some-drops-of-javascript ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 26, 2023 9:59am
roberto-butti commented 11 months ago

Amazing pull request @mahesha1150 . Thank you. Can i ask you (if you have time and you want ) to add another example (in the script in the examples directory) and in the markdown file. I would like to show how to count the occorrences in an aray. Something similar to this one:

const fruits = ['🍓', '🍋', '🍋', '🍎', '🍋', '🍓']

const fruitCounter = fruits.reduce((accumulator, currentFruit) => {
  accumulator[currentFruit] = (accumulator[currentFruit] || 0) + 1
  return accumulator
}, {})

console.log(fruitCounter)

the result is:

{
  "🍓": 2,
  "🍋": 3,
  "🍎": 1
}
mahesha1150 commented 11 months ago

Sure Roberto. While researching I have found additional examples as below in addition to the above one:

Finding the Maximum and Minimum Values: const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];

const max = numbers.reduce((accumulator, currentValue) => { return Math.max(accumulator, currentValue); });

const min = numbers.reduce((accumulator, currentValue) => { return Math.min(accumulator, currentValue); });

console.log("Maximum value:", max); // Output: Maximum value: 9 console.log("Minimum value:", min); // Output: Minimum value: 1

Transforming an Array into a Different Data Structure: const data = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }, ];

const transformedData = data.reduce((accumulator, currentValue) => { accumulator[currentValue.id] = currentValue.name; return accumulator; }, {});

console.log("Transformed Data:", transformedData); // Output: Transformed Data: { 1: 'Alice', 2: 'Bob', 3: 'Charlie' }

But these additional two examples will be making .md file heavy when viewing in browser. So I can skip these two and add only how to count the occurrences in an array

mahesha1150 commented 11 months ago

As mentioned in the above comment, have added additional examples only in example file. Please check and let me know if this is fine.

mahesha1150 commented 11 months ago

image Also not sure, why Hactoberfest team did not accept this PR on reduce().

roberto-butti commented 11 months ago

merged and flagged as hacktoberfest-accepted

mahesha1150 commented 11 months ago

Thank You @roberto-butti. image

Please let me know, if there is anything I can help with.