4

I need to compare 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 compare(array) {
    var isSame = true;
    for(var i=0; i < array.length; i++) {
       isSame = array[0] === array[i] ? true : false;
    }
    return isSame;
}

console.log('compare 1:', compare(myArray1)); // true 
console.log('compare 2:', compare(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
  }
));

5
  • are you only looking at reduce, or also another function like filter? Commented Jan 10, 2017 at 10:03
  • 1
    it's not the actual use case of reduce() method Commented Jan 10, 2017 at 10:04
  • see this:stackoverflow.com/questions/14832603/… 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. Commented Jan 10, 2017 at 10:17
  • 1
    @T.J.Crowder thank you, will update to make that more clear Commented Jan 10, 2017 at 10:21

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you T.J. as usual your explanation is fantastic!
@MichałPerłakowski: That depends on what result you want from an empty array. every always returned true for empty arrays. Whether that's right or wrong depends on the use case.
1

The normal way is to use the .every() functor. If insisted i would come 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.

Comments

-1

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;

9 Comments

I see three possible reasons: 1. The question is about checking if all the values in a single array are the same. The above is comparing myArray1 to myArray2 instead. 2. Comparing arrays by converting them to strings and then doing a lexicographic comparison is not a great idea. 3. a >= b && a <= b is a long-winded way to write a == b.
@T.J.Crowder, nope! a == b compares references and in my case i'm comparing arrays element by element, so you are not right.
Yes, == with arrays would compare references. But rather than converting both arrays to strings twice, the right way to do it lexicographically (which isn't the right way to do it) is var same = String(myArray1) == String(myArray2); (or var same = myArray1.join() == myArray2.join() or similar).
@T.J.Crowder, agree on this, but my answer doesnt deserve downvotes (is this from your side?), it works so i cannot see any reason it's wrong.
@Romko thank you for your time and effort. But it is really not what I was looking for or trying to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.