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

javascript - map function to return 1 boolean value, instead of an array of boolean values - Stack Overflow

programmeradmin2浏览0评论

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); 

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.

Share Improve this question edited Dec 11, 2020 at 7:56 webdesignnoob asked Dec 11, 2020 at 7:49 webdesignnoobwebdesignnoob 3911 gold badge4 silver badges20 bronze badges 3
  • is this supposed to be java or javascript? do understand they are not "one and the same" – Stultuske Commented Dec 11, 2020 at 7:54
  • You need that second return statement, in case there m < 8 – Stultuske Commented Dec 11, 2020 at 7:54
  • @Stultuske javascript, sorry I changed the tag. And that makes sense, thanks for clarifying! – webdesignnoob Commented Dec 11, 2020 at 7:55
Add a comment  | 

3 Answers 3

Reset to default 8

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)

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);

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论