Possible to use a slice in a Sass map loop to start looping at the second instance?

168 views Asked by At

I have a map of breakpoints as follows:

$grid-breakpoints: (
    xs: 0,
    sm: 568px,
    md: 768px,
    lg: 992px,
    xl: 1280px,
    xxl: 1500px
) !default;

And I need to loop over these, however I want to start the loop at the sm breakpoint. Here is my current loop:

@each $name, $value in $grid-breakpoints {
    @include media-breakpoint-up(#{$name}) {
        @for $i from 1 to 13 {

            .. stuff here

        }
    }
}

I haven't been able to find one so far, but is there a way to adjust my opening line above to be something like this:

@each $name, $value in $grid-breakpoints[2:] {

Cheers

2

There are 2 answers

0
Quentin Veron On BEST ANSWER

You could use the map-remove function, which take an ArgList as second parameter.

Demo (SassMeister)

$grid-breakpoints: (
    xs: 0,
    sm: 568px,
    md: 768px,
    lg: 992px,
    xl: 1280px,
    xxl: 1500px
) !default;


@each $name, $value in map-remove($grid-breakpoints, 'xs', 'sm') {
  .. stuff here
}
0
ReSedano On

You could check index number in your loop and if it is > 1 do something:

$grid-breakpoints: (
    xs: 0,
    sm: 568px,
    md: 768px,
    lg: 992px,
    xl: 1280px,
    xxl: 1500px
) !default;

@each $name, $value in $grid-breakpoints {
  $i: index($grid-breakpoints, $name $value);

  @if $i>1{

    do something...

  }
}