Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript early break reduce() method

[ 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 :: how to pause js execution 
Javascript :: opencv rtsp stream python 
Javascript :: loop array reverse 
Javascript :: javascript array to table 
Javascript :: expo build ios 
Javascript :: convert date to readable format javascript 
Javascript :: how to install yup in react native 
Javascript :: chrome storage onchanged 
Javascript :: js sting first letter 
Javascript :: sublime format json 
Javascript :: is var is not blank then display value in javascript 
Javascript :: padstart javascript 
Javascript :: falsy value javascript 
Javascript :: javascript make async get request 
Javascript :: is typescript faster than javascript 
Javascript :: js string to array 
Javascript :: jquery find div with data attribute value 
Javascript :: how to remove timezone from date in javascript 
Javascript :: how to access parent function from iframe 
Javascript :: javascript shuffle string 
Javascript :: filter javascript 
Javascript :: Error: While trying to resolve module `@apollo/client` from file 
Javascript :: jquery table each rows with class 
Javascript :: delete with body angular 
Javascript :: how to turn number into string javascript 
Javascript :: show hide element jquery 
Javascript :: debounchow use input debounce in javascript vue.js 
Javascript :: js select div 
Javascript :: jquery select self and siblings 
Javascript :: how to scroll to an element javascript react 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =