简体   繁体   中英

SCSS @each loop with two variable sets?

Trying SCSS to work with two variable sets, like below, but it keeps returning the crazy sauce below:

 $first: first_1 first_2;
 $second: second_1 second_2
 @each $data in $first {
    @each $content in $second {
    .example-class[data-reactid$='#{$data}'] {
        &:before {
          content: '#{$content}';
        }
    }

}

}

So, it does this:

.example-class[data-reactid$='first_1']:before {
  content: "second_1";
}

.example-class[data-reactid$='first_1']:before {
  content: "second_2";
}

.example-class[data-reactid$='first_2']:before {
  content: "second_1";
 }

.example-class[data-reactid$='first_2']:before {
  content: "second_2";
}

And I want it to just do this:

.example-class[data-reactid$='first_1']:before {
  content: "second_1";
}

.example-class[data-reactid$='first_2']:before {
  content: "second_2";
}

What am I doing wrong?

Rather than use nested loops, what you want to do is use the zip function to zip your lists together. The zip function creates a list of lists where the first, second, etc. element of each list is paired up together:

$first: first_1 first_2;
$second: second_1 second_2;
@each $k, $v in zip($first, $second) {
    .example-class[data-reactid$='#{$k}'] {
        &:before {
            content: '#{$v}';
        }
    }
}

Output:

.example-class[data-reactid$='first_1']:before {
  content: "second_1";
}

.example-class[data-reactid$='first_2']:before {
  content: "second_2";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM