I have a map that looks like:
let mapToSearch =
{
'aaa': '123',
'bbb': '456'
}
and I have a variable:
let keyToFind = 'aaa';
In my HTML template, in an Angular *ngIf
block, I want to check if keyToFind
matches any key within this map.
Something like JS array.indexOf()
:
<ng-container *ngIf="mapToSearch.indexOf(keyToFind)">
Hello world
</ng-container>
Is such a thing possible with maps within a template?
Update (including Solution)
Thanks for all your answers. All your answers are valid for working with a object literal (this was my mistake - I should have declared the map differently in the question).
However, for a ES6 map, I discovered I can use map.has(key)
in the template.
Declare map with map.set()
:
let mapToSearch = new Map();
mapToSearch.set('aaa', '123'};
mapToSearch.set('bbb', '456'};
Template:
<ng-container *ngIf="mapToSearch.has(keyToFind)">
Hello World
</ng-container>
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has