6

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.com/dimolimayi/edit?js,console

I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
  • 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 : https://coderwall.com/p/_ggh2w/the-array-native-every-filter-map-some-foreach-methods – SeleM Dec 25 '16 at 14:54
  • Nothing wrong with using `filter`, but you can use [`.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) to meet your requirements... Related: http://stackoverflow.com/questions/23614054/javascript-nuances-of-myarray-foreach-vs-for-loop – Mottie Dec 25 '16 at 14:56

4 Answers4

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.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • Thank you, to improve your answer - why my solution is wrong that I have used `filter` – I'll-Be-Back Dec 25 '16 at 14:47
  • 1
    You don't *"have to use"* .. it is merely one possible approach – charlietfl Dec 25 '16 at 14:48
  • Other alternative include [`Array.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) & [`Array.includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) – Mottie Dec 25 '16 at 15:01
  • @RajaprabhuAravindasamy Thanks for great answer bro in great detail! ;) – I'll-Be-Back Dec 25 '16 at 15:10
0

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);
ab29007
  • 7,611
  • 2
  • 17
  • 43
0

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 ....

Daniyal Awan
  • 107
  • 5
0

Straightforward approach.

    var data = [
      'one',
      'two',
      'three',
      'four',
      'three',
      'five',
    ];
    
    
    found = data.indexOf('three') == -1 ?  false :  true;
    console.log(found);
sinisake
  • 11,240
  • 2
  • 19
  • 27