M
M
Michael R.2018-05-17 15:22:05
Sass
Michael R., 2018-05-17 15:22:05

Iterating over an array (sass/scss)?

Greetings!
I can't understand a few points in iterating arrays in scss:
1. How to get the number of an array cell in a loop? For example, if we are now in item3, then the number should be 3...
2. How to get the name of the cell in the loop ( item3for example)?
3. How to get the contents of a cell in a loop ( redfor example)?
4. How would you iterate over such an array?
Thank you!

$colors: (
        item1: green,
        item2: orange,
        item3: red,
        item4: blue,
        item5: lime
);

// визуально накидал цикл, чтобы было более нагляднее то, что я хочу узнать...
@for $i from 1 through length($colors)
{
  .test-#{$number-array}-#{$name-array}
  {
    color: #{$value-array};
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iljaGolubev, 2018-05-17
@Mike_Ro

$colors: (
        item1: green,
        item2: orange,
        item3: red,
        item4: blue,
        item5: lime,
);

.test {
  @each $key, $color in $colors {
    $index: index($colors, ($key $color));
    &-#{$index}-#{$key} {
        color: $color;
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question