admin管理员组

文章数量:1279057

I need to pare all values in ONE array to know if they're all equal or not. So this works fine and gives me the expected output

var myArray1 = [50, 50, 50, 50, 50];  // all values are same, should return true
var myArray2 = [50, 50, 50, 50, 51];  // last value differs, should return false

function pare(array) {
    var isSame = true;
    for(var i=0; i < array.length; i++) {
       isSame = array[0] === array[i] ? true : false;
    }
    return isSame;
}

console.log('pare 1:', pare(myArray1)); // true 
console.log('pare 2:', pare(myArray2)); // false

I need to pare all values in ONE array to know if they're all equal or not. So this works fine and gives me the expected output

var myArray1 = [50, 50, 50, 50, 50];  // all values are same, should return true
var myArray2 = [50, 50, 50, 50, 51];  // last value differs, should return false

function pare(array) {
    var isSame = true;
    for(var i=0; i < array.length; i++) {
       isSame = array[0] === array[i] ? true : false;
    }
    return isSame;
}

console.log('pare 1:', pare(myArray1)); // true 
console.log('pare 2:', pare(myArray2)); // false

Then I've tried the same with reduce() but looks like I'm misunderstanding that function. They both say it is false. Am I doing something obviously wrong? Can I use reduce() to get what I need? If so how?

var myArray1 = [50, 50, 50, 50, 50];
var myArray2 = [50, 50, 50, 50, 51];

console.log('reduce 1:', myArray1.reduce(
  function(a, b){
    return a === b ? true : false
  }
));

console.log('reduce 2:', myArray2.reduce(
  function(a, b){
    return a === b ? true : false
  }
));

Share Improve this question edited Jan 10, 2017 at 10:20 caramba asked Jan 10, 2017 at 10:02 carambacaramba 22.5k20 gold badges93 silver badges133 bronze badges 5
  • are you only looking at reduce, or also another function like filter? – A. L Commented Jan 10, 2017 at 10:03
  • 1 it's not the actual use case of reduce() method – Pranav C Balan Commented Jan 10, 2017 at 10:04
  • see this:stackoverflow./questions/14832603/… – Suchit kumar Commented Jan 10, 2017 at 10:10
  • People seem to be interpreting this as checking if myArray1 and myArray2 contain the same values. (I don't know how, but two people have so far.) May be worth emphasizing what you want to do. – T.J. Crowder Commented Jan 10, 2017 at 10:17
  • 1 @T.J.Crowder thank you, will update to make that more clear – caramba Commented Jan 10, 2017 at 10:21
Add a ment  | 

3 Answers 3

Reset to default 8

reduce just isn't the right tool here, the value you return from one iteration is used as a in the next iteration, and reduce doesn't short-circuit.

If you want to use one of the array methods to do this, every would be a reasonable choice:

var myArray1 = [50, 50, 50, 50, 50];
var myArray2 = [50, 50, 50, 50, 51];

console.log('some 1:', myArray1.every(
  function(value, _, array){
    return array[0] === value;
  }
));

console.log('some 2:', myArray2.every(
  function(value, _, array){
    return array[0] === value;
  }
));

every short-circuits, stopping as soon as the result is known.

I mean, you could shoe-horn it into a reduce, but it's not appropriate. We initialize the flag with true and then propagate the result of &&'ing it with checking that entry against the array's first entry:

var myArray1 = [50, 50, 50, 50, 50];
var myArray2 = [50, 50, 50, 50, 51];

console.log('some 1:', myArray1.reduce(
  function(flag, value){
    return flag && myArray1[0] === value;
  },
  true
));

console.log('some 2:', myArray2.reduce(
  function(flag, value){
    return flag && myArray2[0] === value;
  },
  true
));

The normal way is to use the .every() functor. If insisted i would e up with a .reduce() solution as follows;

var arr = [50,50,50,50,50],
    brr = [50,50,51,50,50],
    res = arr.reduce((p,c) => p === c ? p : false);
console.log(res);
res = brr.reduce((p,c) => p === c ? p : false);
console.log(res);

It will return the element that's the array is filled with or false if there is a black sheep. The above code will fail if your input array is full of falses.

try (if they are numeric) this way

var myArray1 = [50, 50, 50, 50, 50];
var myArray2 = [50, 50, 50, 50, 51];

var same = myArray1 >= myArray2 && myArray1 <= myArray2;

本文标签: javascriptCheck if all array values are the same with reduce()Stack Overflow