0

I only want to show unique variables. Is there an easier or cleaner way to compare three js variables?

if (a==b) {
  if (a==c) {
   //show only a
  } else {
    //show a and c
  }
} else {
  if (a==c) {
    //show a and b
  } else {
    //show a and b and c
  }
}
Brad
  • 1,019
  • 1
  • 9
  • 22

2 Answers2

1

Not really, no:

if (a == b && a == c) {
    // show a
} else if (a == b) {
    // show a and c
} else if (a == c) {
    // show a and b
} else {
    // show a, b, and c
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

I know you have not specified to use jQuery, but this is another way you could do it (should you choose to use Jquery)

var array1 = [];
var a = 2, b = 3, c= 2;
array1.push(a);
array1.push(b);
array1.push(c);

console.log(array1);

var unique = $.unique(array1);

console.log(unique);

P.S. - I have considered the variables to be numbers.

gkb
  • 1,449
  • 2
  • 15
  • 30
  • Thanks, I am using jQuery, but the variables are strings. Does $.unique() work on strings too? – Brad Oct 21 '16 at 10:46
  • As per the documentation - https://api.jquery.com/jQuery.unique/, it does not, but as per this post - http://stackoverflow.com/questions/10191941/jquery-unique-on-an-array-of-strings, it might work.. – gkb Oct 21 '16 at 10:50
  • It isn't documented to support either strings *or* numbers. In fact, it's documented **not** to: *"Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, **not strings or numbers**."* – T.J. Crowder Oct 21 '16 at 10:55
  • You are right @T.J.Crowder. Came to know about it later when I went through the doc again. Thanks.. – gkb Oct 21 '16 at 10:59