SASS - get map item value by item index

2019-02-14 07:16发布

问题:

I want to be able to pick a SASS map item value by that map item's index.
Simplified scenario:

SCSS

// Colors map (MUST stay this way due to system dependence)
$colors: (
    a: red,
    b: green,
    c: blue
);

@for $i from 1 through 3{
    a:nth-child({$i}){ color:[GET COLOR BY $i FROM $COLORS]; }
}

Is that possible?

回答1:

gist demo

$colors: (
    a: red,
    b: green,
    c: blue
);

@each $color, $name in $colors{
  $i: index($colors, $color $name);
  a:nth-child(#{$i}){ color:$color; }
}


标签: sass