最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS exit a .map() function - Stack Overflow

programmeradmin1浏览0评论

So I'm using a map function to iterate and array, and I want to stop going trough elements inside a map function once a condition has been met.

Is there a way to do so ?

Ex :

var arr = [1,2,3,4,5,6,7,8,9,10];
arr.map((el,index)=>{
  console.log("Element : ",el)
  if(el > 5){
    //Stop the map function and proceed to the next operation
    //like goto line 11 so skip going trough the map anymore
    console.log("Stop")
  }
})

console.log("Sucess!")

So I'm using a map function to iterate and array, and I want to stop going trough elements inside a map function once a condition has been met.

Is there a way to do so ?

Ex :

var arr = [1,2,3,4,5,6,7,8,9,10];
arr.map((el,index)=>{
  console.log("Element : ",el)
  if(el > 5){
    //Stop the map function and proceed to the next operation
    //like goto line 11 so skip going trough the map anymore
    console.log("Stop")
  }
})

console.log("Sucess!")

So in this example I just want to stop the map once the element inside it, has a value bigger than 5. Is there a way to do this with map, or I should find another solution ?

Why map ? -Seems like an intresting solution, and I was wondering if it's possible to use in y case

Share Improve this question edited Dec 5, 2019 at 10:37 Ionut Eugen asked Dec 5, 2019 at 10:29 Ionut EugenIonut Eugen 5012 gold badges6 silver badges28 bronze badges 3
  • 1 Don't use a map "sort of like a for function". Use a loop if you want to be able to stop it. – deceze Commented Dec 5, 2019 at 10:31
  • what is goto:11? btw why map? you don't use the result of it ...? – Nina Scholz Commented Dec 5, 2019 at 10:31
  • Nina goto:11 is a function that older languages used in order to skip a piece of code and go to a certain line of code. In this case 11. If I remember right one of such languages is assembler. – Ionut Eugen Commented Dec 5, 2019 at 10:41
Add a ment  | 

2 Answers 2

Reset to default 9

You are not looking for .map function, but you need to use .some or .find

var arr = [1,2,3,4,5,6,7,8,9,10];
arr.some((el,index)=>{
  console.log("Element : ",el)
  if(el > 5){
    //Stop the map function and proceed to the next operation
    //like goto:11
    console.log("Stop")
    return true // stop the iteration of the first "truty" value
  }
  return false
})

console.log("Sucess!")

Use a for...of loop, if you don't need the index:

var arr = [1,2,3,4,5,6,7,8,9,10];
for (const el of arr) {
  console.log("Element : ",el)
  if(el > 5){
    //Stop the map function and proceed to the next operation
    //like goto:11
   console.log("Stop")
   break;
  }
}

console.log("Sucess!")


Why map ? -Seems like an intresting solution, and I was wondering if it's possible to use in y case

You can also use a screwdriver with a thick handle to drive in a nail, but it's still the wrong tool for the job ;)

The only way to stop iteration with .map would be to throw an exception. You could do something like this:

var arr = [1,2,3,4,5,6,7,8,9,10];
var sentinel = {};

try {
  arr.map((el,index)=>{
    console.log("Element : ",el)
    if(el > 5){
      //Stop the map function and proceed to the next operation
      //like goto line 11 so skip going trough the map anymore
      console.log("Stop")
      throw sentinel;
    }
  })
} catch(e) {
  if (e !== sentinel) throw e;
}

console.log("Sucess!")

But that isn't better in any way.

发布评论

评论列表(0)

  1. 暂无评论