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

javascript - How to stop looping once it found? - Stack Overflow

programmeradmin0浏览0评论

If three is found then it should return true and stop the iteration. Otherwise return return false if not found.

I am using filter() - is it wrong approach to use?

var data = [
  'one',
  'two',
  'three',
  'four',
  'three',
  'five',
];

found = data.filter(function(x) {
   console.log(x);
   return x == "three";
});

console.log(found);

Demo: ,console

If three is found then it should return true and stop the iteration. Otherwise return return false if not found.

I am using filter() - is it wrong approach to use?

var data = [
  'one',
  'two',
  'three',
  'four',
  'three',
  'five',
];

found = data.filter(function(x) {
   console.log(x);
   return x == "three";
});

console.log(found);

Demo: https://jsbin./dimolimayi/edit?js,console

Share Improve this question asked Dec 25, 2016 at 14:43 I'll-Be-BackI'll-Be-Back 10.8k42 gold badges118 silver badges227 bronze badges 2
  • 1 filter() method, Runs the give function on every item in the array and returns an array of all items for which the function returns true; Useful link : coderwall./p/_ggh2w/… – SeleM Commented Dec 25, 2016 at 14:54
  • Nothing wrong with using filter, but you can use .find() to meet your requirements... Related: stackoverflow./questions/23614054/… – Mottie Commented Dec 25, 2016 at 14:56
Add a ment  | 

4 Answers 4

Reset to default 7

You can use array#some at this context,

var data = [
  'one',
  'two',
  'three',
  'four',
  'three',
  'five',
];

found = data.some(function(x) {
   return x == "three";
});

console.log(found); // true or false

If you use filter, then the array will be filtered based on the truthy value returned inside of the callBack function. So if any matches found meaning, if the function returned with value true, then the element on that particular iteration will be collected in an array and finally the array will be returned.

Hence in your case ["three", "theree"] will be returned as a result. If you dont have any "three", then an empty array would be returned, At this context you have to make a additional check to find the truthy value as a result.

For Example:

var res = arr.filter(itm => someCondition);
var res = !!res.length;
console.log(res); //true or false.

So to avoid that over killing situation we are using Array#some.

var data = [
  'one',
  'two',
  'three',
  'four',
  'three',
  'five',
];

for(var i=0;i<data.length;i++){
    console.log(data[i]);
  if(data[i]=="three"){
    var found=data[i];
    break;
  }
}

console.log(found);

You have to return false to get out of function but you are already using return statement in filter function and you can not use 2 return statements ... One other solution i came up with is this :

var data = [
  'one',
  'two',
  'three',
  'four',
  'three',
  'five',
];

var filteredData = [];

function a(i,e){
   console.log(e);

  if(e == "three"){
    filteredData.push(e);
    return false;
  }
}

$(data).each(a);
console.log(filteredData);

This will break out as soon as it hits "three" and also stores it in filteredData array so you can use it in future ... Hope this helps ....

Straightforward approach.

    var data = [
      'one',
      'two',
      'three',
      'four',
      'three',
      'five',
    ];
    
    
    found = data.indexOf('three') == -1 ?  false :  true;
    console.log(found);

发布评论

评论列表(0)

  1. 暂无评论