I'm attempting to insert a specific value into a map at a designated key.
My approach involves using ngFor
to iterate through an array. The item obtained in each iteration should serve as the key for the map, while the user enters the corresponding value.
NOTE: The variable userInputValue
is currently non-existent. This is precisely what I am questioning: How can I obtain the userInputValue
to assign it to the map? Hopefully my explanation makes sense...
The item
represents any string, such as "Name"
, and userInputValue
would be whatever the user inputs into the input field
. How can I retrieve this value in this scenario?
In my Component.html
<form *ngFor="let item of myStringArray">
<input [(ngModel)]="map.set(item, userInputValue)">
</form>
In my Component.ts
public map = new Map<string, string>();
public myMethod() {
this.stringInputs.forEach((item) => {
this.entry.roomSizeSQM = this.map.get(item);
});
}
Is it feasible to utilize a Map in this manner? If so, how can I do it correctly, and if not, are there alternative approaches I could take to achieve this goal (aside from creating my own class)?
Currently, my solution involves delimiter splitting and appears as follows:
// the string might be something like: "Name;34"
this.finalInputWithContext.forEach((item) => {
if (item.split(";")[0] === "Name") {
console.log(item.split(";")[1]); // will display 34
}
});
This method works fine, but I have been advised to use a Map instead...