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
  }
}

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

At least this is a bit cleaner than mine! Thanks!
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.

4 Comments

Thanks, I am using jQuery, but the variables are strings. Does $.unique() work on strings too?
As per the documentation - api.jquery.com/jQuery.unique, it does not, but as per this post - stackoverflow.com/questions/10191941/…, it might work..
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."
You are right @T.J.Crowder. Came to know about it later when I went through the doc again. Thanks..

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.