Add support for accessing keys in mapValues() function
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Description:**
Currently, the `mapValues()` function in Bicep allows iterating over an object and transforming its values, but it does not provide access to the *keys* of the object entries. This limits the function’s utility for scenarios where both the key and the value are needed in the transformation logic.
**Proposed Enhancement:**
Add support for an optional second parameter to the `mapValues()` callback to expose the key of each property being iterated over. This would mirror the behavior of array iteration in Bicep, which allows access to both the item and the index.
**Example Syntax:**
```bicep
var definition = {
first: { value: '1' }
second: { value: '2' }
}
var displayNames = {
first: '1st'
second: '2nd'
}
var mappedValues = mapValues(definition, (key, value) => {
...value
displayName: displayNames[key]
})
```
**Benefits:**
* Aligns `mapValues()` with other Bicep constructs that expose both value and metadata (e.g., index in arrays).
* Improves readability and maintainability of templates that work with key-value pairs.
**Workaround Today:**
Currently, users must convert the object to array, add key to the new object and then remove it from the final result., for example:
```bicep
var mappedValues = toObject(
map(items(definition), x => {
_key: x.key
...x.value
displayName: displayNames[x.key]
}),
k => k._key,
v => toObject(filter(items(v), x => x.key != '_key'), k => k.key, v => v.value)
)
```
This works, but it's more verbose and less intuitive than using `mapValues()` directly.
**Request:**
Please consider enhancing `mapValues()` to allow key access in its lambda expression. This small change would unlock significant flexibility and improve developer experience.
Contributor guide
Research direction
Start by locating the mapValues() implementation and its lambda parameter handling in the Bicep codebase. Trace how object iteration is compiled and find the existing tests for mapValues(); done means callbacks can receive each property key while existing one-parameter usage continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure
- Domain
- infrastructure
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100