I have array of 10 elements, I am mapping to extract array data . I want to stop array executing when compile reached to array index 5 . I know it possible with for loop but I need it in map function. Please help me
Actually you can stop execution using [1,2,3,4,5,6,7,8,9,10].map((x,i) => { if(i>=5) throw 0; console.log(x**2) }) - however throw exception will slow down your code and they are not designed for break loops in normal situations
mapcan't do that but you can achieve that withreduceorfilterreduceandfilter?sliceandmap?[1,2,3,4,5,6,7,8,9,10].map((x,i) => { if(i>=5) throw 0; console.log(x**2) })- however throw exception will slow down your code and they are not designed for break loops in normal situations