list.insert-nth()

The insert-nth() function returns the $list with $value inserted into index $n.

// Sass.
@use 'sass:list';

// The `list.insert-nth()` function.
@function insert-nth($list, $n, $value) {
  $result: ();
  @for $i from 1 through list.length($list) {
    @if $n == $i {
      $result: list.append($result, $value, list.separator($list));
    }

    $result: list.append($result, list.nth($list, $i), list.separator($list));
  }
  @return $result;
}

Parameters

$list

The list to insert $value at index $n.

$n

The index $n under which $value is inserted.

$value

The value to insert at the $n index.

Return

The return value is the list with the inserted $value at the $n index.

Examples

// Use.
@use 'angular-package/sass/list';

// Examples.
@debug list.insert-nth(('a', 'b', 'd', 'e', 'f', 'g'), 3 , 'c'); // "a", "b", "c", "d", "e", "f", "g"
@debug list.insert-nth('a' 'b' 'd' 'e' 'f' 'g', 3, 'c'); // "a" "b" "c" "d" "e" "f" "g"
@debug list.insert-nth(('a' 1) ('b' 2) ('d' 4) ('e' 5) ('f' 6) ('g' 7), 3, ('c' 3)); // ("a" 1) ("b" 2) ("c" 3) ("d" 4) ("e" 5) ("f" 6) ("g" 7)

Last updated