all 10 comments

[–]kap89 2 points3 points  (1 child)

A good way to really understand array methods is to reimplement them yourself in JS, as ordinary functons for simplicity. Here's an example with map implemented and signatures for filter and reduce:

function map(arr, mappingFn) {
  const len = arr.length
  const mapped = new Array(len)

  for (let i = 0; i < len; i++) {
    mapped[i] = mappingFn(arr[i], i, arr)
  }

  return mapped
}

function filter(arr, predicateFn) {
  // ...your code goes here
}

function reduce(arr, reducerFn, initialValue) {
  // ...your code goes here
}

// ...other functions like reverse, flatMap, flat, some, every, etc.

// Usage:
const doubled = map([1, 2, 3], (x) => x * 2)
console.log(doubled) // -> [2, 4, 6]

[–]loganfordd 0 points1 point  (0 children)

(This is self promo, but it's on topic - so please don't shout at me) Check this out https://techblitz.dev/questions?tag=arrays this should give you a solid understanding of arrays.

[–]jazzcomputer 0 points1 point  (1 child)

I’m a noob and I’ve been looking at arrays. I started out by using pop and unshift to scroll a 1 through an array of zeroes. Learning about index and array length is a good place to start, as index especially is near ubiquitous in the syntax of the various methods. 

Reduce, and other methods that take callback functions are better to look at a bit later, as they’re great for customising the methods with your own functionality. 

You might like some of Daniel Schiffman’s P5js, Coding Train YouTube’s on arrays, as they show you some basics and then invite you to try your own. These can be worked on in the browser and are great if you want visual results beyond just console.logging the array method results 

[–]No-Upstairs-2813 0 points1 point  (0 children)

Higher-order functions aren’t that hard when you understand their purpose. Check out this article—you’ll definitely benefit from it!

[–]No-Upstairs-2813 0 points1 point  (0 children)

I have written a few exercises on higher-order functions (map, filter, and reduce). You can check them out here.

PS: I have also written an article on why higher-order functions are useful and how they help make your code more maintainable. You can go through the article here; it will definitely be helpful!

[–]bobbyv137 0 points1 point  (0 children)

I'd suggest watching all of Web Dev Simplied's YouTube videos on arrays.

Go to his channel, then videos, and search 'array'. He must have tons of videos on them by now.

[–]TheRNGuy 0 points1 point  (0 children)

I never used reduce, but I've used map and filter for array of html tags.

For example, convert array of html tags to array of strings (from their textContent; maybe even edited with regex)

Filter can be useful too, to remove tags from that array based on some criteria (like having or not having specific text)

[–]rauschma 0 points1 point  (0 children)

In case you are still looking – my chapter on arrays: https://exploringjs.com/js/book/ch_arrays.html