Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

reduce() break

[ 1, 2, 3, 4, 5].reduce((sum, el, _, arr) => {
  if (el === 4) {
    arr.length = 0;//Array passed to callback is now empty

    return sum;
  }

  return sum + el;
}); // return 1 + 2 + 3 = 6
Comment

javascript - How to early break reduce() method?

const array = ['apple', '-pen', '-pineapple', '-pen'];
const x = array
    .slice(0)                         // create copy of "array" for iterating
    .reduce((acc, curr, i, arr) => {
       if (i === 2) arr.splice(1);    // eject early by mutating iterated copy
       return (acc += curr);
    }, '');

console.log("x: ", x, "
original Arr: ", array);
// x:  apple-pen-pineapple
// original Arr:  ['apple', '-pen', '-pineapple', '-pen']
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert data uri to image file javascript 
Javascript :: how to use Space for vertically in antd 
Javascript :: js get clipboard data 
Javascript :: js print all prime numbers 
Javascript :: vowel array 
Javascript :: jquery empty 
Javascript :: styled components import google font 
Javascript :: list all functions in an object js 
Javascript :: js escape characters 
Javascript :: fetch data in next js 
Javascript :: quine 
Javascript :: How can I know which radio button is selected via jQuery 
Javascript :: angular refresh token 
Javascript :: generate random id 
Javascript :: javascript replace dash with space 
Javascript :: javascript refresh page 
Javascript :: index of value in array 
Javascript :: js capitalize first letter 
Javascript :: get element by id in jquery 
Javascript :: convert array to number js 
Javascript :: js reduce a array of straing 
Javascript :: react keydown event listener 
Javascript :: javascript class extends 
Javascript :: lodash empty string 
Javascript :: regex is not empty string 
Javascript :: bootstrap disable button after click 
Javascript :: useeffect clearinterval loading 
Javascript :: how to select div js 
Javascript :: mongodb $in regex 
Javascript :: jquery after 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =