1

Javascript question.

As title says, is it OK manner to set boolean values(true or false) to an array and check if either one of those exits in array afterwards?

Let's say I have function that returns either true or false.

Sample code:(using jQuery)

var abort = [];//make an empty array

function someFunc() {
    var returnVal = false;

    if( some condition ) {
        returnVal = true;
        return returnVal;
    }

    return returnVal;
}

someElement.each(function() {
    abort.push( someFunc() );    //push true or false in array
});
//abort array will look eventually something like...
//[true, false, true, false, false, true, ...]

//check if `true` exists in array jQuery approach
var stop = ( $.inArray(true, abort) > -1) ? true : false ;

if( stop ) {
    console.log('TRUE FOUND AT LEAST ONE IN ARRAY!');
}

This seems working OK. But I was just wondering if this is the right way...

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
norixxx
  • 2,549
  • 2
  • 19
  • 26
  • Why do you even think it wouldn't be OK to add booleans to an array? You can put any value into an array! – Felix Kling Jan 23 '14 at 08:17
  • Perfectly ok. I'd shorten it to `function somefunc() { return(some condition)}`, though. And even more general: Are you using the different `Boolean`s in `abort` later on? If not, a single global is sufficient: `someElement.each(function() {if (! stop) {stop = someFunc()};});` in this case replaces the `$.inArray` stuff as well. – virtualnobi Jan 23 '14 at 08:19
  • how many booleans do you need ? because a single integer might be handy to use and have great performances if you are using less than 31 booleans. You might have a look at my answer on that question for instance : http://stackoverflow.com/questions/18547164/how-to-use-unit8array-in-javascript/18548027#18548027 – GameAlchemist Jan 23 '14 at 13:20

1 Answers1

0

If you don't want to invoke all the functions, if any of the functions return true, you can use native Array.prototype.some method like this

if (someElement.some(function(currentObject) {
    return <a bool value>;
}))) {
    console.log("Value exists");
} else {
    console.loe("Value doesn't exist");
}

For example,

var someArray = [1,5,6,8,3,8];

if(someArray.some(function(currentObject) {
    return currentObject === 3;
})) {
    console.log("3 exists in the array");
} else {
    console.log("3 does not exist in the array");
}

would print

3 exists in the array

If you want to execute all the functions irrespective of all the results but if you want to know if atleast one of them returned true, then you can use Array.prototype.reduce, like this

var someArray = [1,5,6,8,4,8];

function printer(currentObject) {
    console.log(currentObject);
    return currentObject === 3;
}

if(someArray.reduce(function(result, currentObject) {
    return result || printer(currentObject);
}, false)) {
    console.log("3 exists in the array");
} else {
    console.log("3 does not exist in the array");
}

Output

1
5
6
8
4
8
3 does not exist in the array
thefourtheye
  • 233,700
  • 52
  • 457
  • 497