3

Say you have an array like this:

arrayExample = [1, 2, 3, 4, 5, 6, 7]

I want to use the map function to iterate through arrayExample and return true if all the numbers are less than 8, false if they are not. However, when I do this I get an array of trues (like: [true, true, true... etc])

Would I be able to return just 1 value?

Here is my code so far:

var testBoolean = true;
var array = [1,2,3,4,5,6,7];

testBoolean = array.map(m => { 
    //if a number in the array is >=8, changes the testBoolean to false and return false only
    if(m >= 8) 
    { 
        return false;
    }
    //VS code said I had to have a return here, I believe its because I need to have in case m < 8 (edited after reading a comment)
    return true;
 })
 
 //prints an array [true,true,true,true,true,true,true]
document.write(testBoolean); 

I'm a bit new to "map" but I believe it does this since it returns a value for every element, just confused on how to make it so it returns 1 true or false.

webdesignnoob
  • 381
  • 1
  • 4
  • 13

3 Answers3

7

The simplest solution is to use .some().

We don't need to check every value. We need to find the first value not less than 8.

const array = [1, 2, 3, 4, 5, 6, 7]
const testBoolean = !array.some(m => m >= 8)
console.log(testBoolean)
ecoplaneteer
  • 1,918
  • 1
  • 8
  • 29
5

For something like this .map() isn't the right tool. .map() is for converting all the elements in your array to new elements (ie: mapping each element to a new transformed value). As a result, .map() will always return an array (unless this behaviour is intentionally modified). Instead, you can use .every() for this, which will tell you if all elements in your array match your condition (ie: if your function returns true for every element, then you'll get true as your result, otherwise you'll get false). The .every() method will terminate early as soon as it finds one element which your callback function returns false for, which can help with efficiency:

const array = [1, 2, 3, 4, 5, 6, 7];
const testBoolean = array.every(m => {
  if (m >= 8) {
    return false;
  }
  return true;
});
console.log(testBoolean);

This can be written more concisely, by just returning the result of m < 8 (this will either evaluate to true or false)

const array = [1,2,3,4,5,6,7];
const testBoolean = array.every(m => m < 8);
console.log(testBoolean);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • @NickParsons I strongly recommend using`.some()` for better performance since every value doesn't need to be checked. – ecoplaneteer Dec 11 '20 at 08:22
  • 1
    @DevMaster `every` also short-circuits like `some` – adiga Dec 11 '20 at 08:25
  • @DevMaster As adiga pointed out, `.every()` doesn't (always) check every value, it terminates early just like `.some()` does. As soon as `every()` finds a value which is false, it will stop and return false. `.some()` works in the opposite way, as soon as it finds a value that is `true`, it stops and returns `true`. If it never finds a value which `true` is returned for though, it will need to iterate the entire array – Nick Parsons Dec 11 '20 at 08:26
  • @DevMaster Thank you for the other option of using .some(). I chose Nick's option since the project I'm working on requires me to check every element. A bit of a different scenario to what I put as my example but the same idea! I'll keep the .some() function in mind though. Thanks again to both of you! – webdesignnoob Dec 11 '20 at 08:26
  • 1
    @DevMaster you can easily verify it: You can run `[1,2,3,4,5].every(a => { console.log(a); return a < 3 })` or using the comma operator `[1,2,3,4,5].every(a => (console.log(a), a < 3) )`. It will only log until `3`. Once that doesn't satisfy the condition, it doesn't need to check any further. – adiga Dec 11 '20 at 08:34
1

From a more general perspective, if you want to return a single value from an Array, .reduce() is your friend.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Specifically in your case, you could do

array.reduce(function(accumulator, currentVal, i, arr) {
  return accumulator && currentVal < 8;
}, true));

So .reduce() iterates through your array with an initial value true and returns the "previous" value (accumulator) AND whether the current value is less than 8.

You could think of it as

(true && (a[0] < 8 && (a[1] < 8 ... )))

The returned value at each iteration becomes the accumulator of the next. In this way you could not only do math operations but also change an array of a certain shape (e.g. array of {w:0, h:0} objects) to an output of another (e.g. a single number being the sum of all the hypotenuses calculated from each w and h).