As the title says I need to update one value in array.
<div *ngFor="let item of data; let i = index" >
<div class="full-w" fxLayout='row' fxLayoutAlign='start center' fxLayoutGap='12px'>
<span class="title">Color: </span>
<span>{{item.color}}</span>
</div>
<mat-divider class="full-w"> </mat-divider>
<div *ngFor="let e of item.sizeQuantity; let j = index" fxLayout='row' fxLayoutAlign='space-between center'
fxLayoutGap='12px'>
<span class="title">Size: </span>
<span class="size">{{e.size}}</span>
<mat-form-field>
<input matInput type="number" placeholder="Quantity" [value]="e.quantity" #quantity />
</mat-form-field>
<button mat-button (click)="updateQuantity(quantity.value, item, i, j)">Add</button>
</div>
</div>
and this is the initial data
and when I enter the data in one field, e.g. under black>xs, it changes on beige>xs too (if there is blue>xs or more it gets updated)
I tried several approaches but it keeps updating values at every position
updateQuantity(value, item, ind, j) {
this.data.forEach(e => {
if (e.color == item.color) {
e.sizeQuantity[j].quantity = parseInt(value);
}
})
}
Or like this
updateQuantity(value, item, ind, j) {
this.data.find(e => e.color == item.color).sizeQuantity[j].quantity = parseInt(value);
}
Am I missing something?
//EDIT:
Data is created like this:
let quantity = [];
let sizeQuantity = [];
this.selectedSizes.forEach(e => {
sizeQuantity.push({ size: e, quantity: 0 })
})
this.selectedColors.forEach(e => {
quantity.push({ color: e, sizeQuantity })
})
this.dialog.open(AddQuantityComponent, {
data: quantity
})
where sizes and colors arrays are dynamically created based on user selection:
this.selectedColors = ['black', 'beige', 'blue'];
this.selectedSizes = ['XS', 'S'];



this.dataget its initial value inadd-quanitity.component.ts? I suspect that the initial values of the colors are in one array instance and the colors contain reference to that instance. And as the quantity at one color is updated, the original array is updated and because of this all the colors get the same quantity value.this.selectedSizesandthis.selectedColorsare inversely. I assumed in my answer thatthis.selectedSizes =['XS', 'S'];andthis.selectedColors = ['black', 'beige', 'blue'];.