Accessing an array key in SASS

前端 未结 4 1782
鱼传尺愫
鱼传尺愫 2020-12-05 00:43

I have a list in SASS, and I\'m trying to access on of the items by using bracket notation:

$collection[1];

but that gives me an error.

4条回答
  •  感情败类
    2020-12-05 01:06

    $color-collection: ('red', 'orange', 'green', 'blue');
    
    @for $i from 0 to length($color-collection) {
        .color-#{$i} {
            color: unquote(nth($color-collection, $i+1));
        }
    }
    

    Use nth(), also unquote() if you want to pass quoted strings.

    Though, I personally wouldn't:

    $color-collection: (red, rgba(50,50,80, .5), darken(green, 50%), rgba(blue, .5));
    
    @for $i from 0 to length($color-collection) {
        .color-#{$i} {
            color: nth($color-collection, $i+1);
        }
    }
    

    Because then you can pass any color object.

提交回复
热议问题