# map.key-replace()

The `map.key-replace()` function returns the map with key [`$replace`](#usdreplace) replaced by [`$replacement`](#usdreplacement).

{% code lineNumbers="true" %}

```scss
// Sass.
@use 'sass:map';

// Functions.
@use '../string';

// The `map.key-replace()` function.
@function key-replace($map, $replace, $replacement) {
  @if type-of($map) == map {
    @each $key, $value in $map {
      $map: map.remove($map, $key);
      @if type-of($key) ==
        string and
        type-of($replace) ==
        string and
        type-of($replacement) ==
        string
      {
        @if string.index($key, $replace) {
          $key: string.replace($key, first, $replace, $replacement);
        }
      } @else if $key == $replace {
        $key: $replacement;
      }

      $map: map.merge(
        $map,
        (
          $key: $value,
        )
      );
    }
  }
  @return $map;
}
```

{% endcode %}

{% embed url="<https://github.com/angular-package/sass/blob/main/map/_map.key-replace.function.scss>" %}

### Parameters

#### `$map`

A map in which a [`$replace`](#usdreplace) key is replaced with [`$replacement`](#usdreplacement).

#### `$replace`

A key, or if `string` substring to replace with [`$replacement`](#usdreplacement) in [`$map`](#usdmap).

#### `$replacement`

The value to replace a [`$replace`](#usdreplace) key in [`$map`](#usdmap).

### Return

The return value is a [`$map`](#usdmap) with replaced key [`$replace`](#usdreplace) by [`$replacement`](#usdreplacement).

## Examples

```scss
// Use.
@use '@angular-package/sass/map';

// Examples.
$-map: (
  key replace: (map: 1),
  key-replace: (map: 2),
);

// replace string key
@debug map.key-replace($-map, key-replace, new-key); // (key replace: (map: 1), new-key: (map: 2))
@debug map.key-replace(('bold king': 1, b: 2, c: 3), 'bold', 'hairy'); // ("hairy king": 1, b: 2, c: 3)
@debug map.key-replace(('bold king': 1, b: 2, c: 3), 'o', 'ooo'); // ("booold king": 1, b: 2, c: 3)
@debug map.key-replace((a: 1, b: 2, c: 3), 'a', 'd'); // (d: 1, b: 2, c: 3)

// replace string key with a list key
@debug map.key-replace($-map, key-replace, new key); // (key replace: (map: 1), new key: (map: 2))

// replace list key
@debug map.key-replace($-map, key replace, 'd'); // (d: 1, b: 2, c: 3)

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.angular-package.dev/sass/map/map.key-replace.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
