Search code examples
loopssasseachslicesass-maps

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


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


Solution

  • 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
    }