Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write "IF value IN array"?

Hi I'm new to SASS and want to know how to write something like "if value in array"

For example:

$animal : dog;
$landAnimal : (dog, cat, lion);
$seaAnimal : (fish, squid, whale);

//keyword IN doesn't work
@if $animal in $landAnimal{
    //do something
}@else if $animal in $seaAnimal{
    //do something
}

I tried searching the documentation and googling but I haven't found any answers. Anyone here have solution? I believe it is quite simple.

like image 552
hrsetyono Avatar asked Mar 13 '13 14:03

hrsetyono


People also ask

How do you write an if statement for an array?

For example, if condition «a» is an array of Booleans (true or false values), it returns an array with the same index, containing «b» or «c» as appropriate: Variable X := -2 .. 2. If X > 0 THEN 'Positive' ELSE IF X < 0 THEN 'Negative' ELSE 'Zero' →

How do you check if a value is in an array C?

To check if given Array contains a specified element in C programming, iterate over the elements of array, and during each iteration check if this element is equal to the element we are searching for.

How do you check if a value exists in an array JS?

The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise.


1 Answers

You're looking for the index function: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#index-instance_method

@if index($landAnimal, $animal) {
    //do something
} @else if index($seaAnimal, $animal) {
    //do something
}
like image 95
cimmanon Avatar answered Nov 15 '22 14:11

cimmanon