JavaScript iterations method
JavaScript 3 statement for looping: 1.For...of loop : used to get the values of an array one by one. For array we prefer for...of to get value. let nums = [ 10 , 20 , 30 ]; for ( let x of nums ){ console . log ( x ); } output : 10 20 30 2.for...in loop : used to access the keys or indexes of an array. (Not recommended) let nums = [ 10 , 20 , 30 ]; for ( let i in nums ){ console . log ( i ); }…
JavaScript provides several methods for iterating over arrays. Three primary methods are for...of, for...in, and forEach().
The for...of loop is used to retrieve the values of an array one by one. For instance, using an array declared as let nums = [10, 20, 30], the for...of loop can be employed as follows: for (let x of nums) { console.log(x) }. This results in output of 10, 20, and 30.
The for...in loop is utilized for accessing the keys or indexes of an array. However, it is generally not recommended for use with arrays. An example would be: for (let i in nums) { console.log(i) }.
The forEach() method executes a function for each array element. It does not create a new array. For example, with const numbers = [45, 4, 9, 16, 25], the forEach() method can be used like this: numbers.forEach(myFunction); where myFunction(value) logs the value.
The map() method creates a new array with the results of calling a provided function on every element in the calling array. For instance, with num1 = [1, 2, 3], and a function myFunction(value) that returns value * 2, num1.map(myFunction) would result in [2, 4, 6].
The flatMap() method first maps each array element using a mapping function, then flattens the result into a new array. For example, with nums = [1, 2, 3], and a function that returns [n, n * 10], nums.flatMap(n => [n, n * 10]) would produce [1, 10, 2, 20, 3, 30].
The filter() method creates a new array with all elements that pass the test implemented by the provided function. For example, with nums = [10, 15, 20, 25], nums.filter(myFunction) where myFunction(value) returns value > 15, would result in [20, 25].
The reduce() method reduces the array to a single value. It works from left to right. For example, with nums = [10, 20, 30], nums.reduce((total, n) => total + n, 0) would result in 60.
The reduceRight() method is similar to reduce(), but it works from right to left. The every() method checks if all elements satisfy a condition, returning true or false. For instance, with nums = [10, 20, 30], nums.every(n => n > 5) would return true, while nums.every(n => n > 15) would return false.
The some() method checks if at least one element satisfies a condition, returning true or false. Using the same nums array, some(nums, n => n > 5) would return true, as would some(nums, n => n > 15). Lastly, Array.from() creates a new array from an iterable or array-like value. For example, Array.from(text) where text is 'CAT', would result in ['C', 'A', 'T'].
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.