0

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

8
  • map can't do that but you can achieve that with reduce or filter Commented Aug 21, 2019 at 19:14
  • @Alireza can you please make a simple array and apply this function Commented Aug 21, 2019 at 19:22
  • You mean reduce and filter? Commented Aug 21, 2019 at 19:22
  • Why not just map the whole array till the 5th index with a combination of slice and map? Commented Aug 21, 2019 at 19:26
  • 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 Commented Aug 21, 2019 at 19:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.