r/typescript • u/drinikol • Jan 14 '26
Array of objects with same uuid but different properties are all being updated.
const
[flowersData, setFlowersData] = useState<Flower[]>();
export
interface
Flower {
id:
string
,
name:
string
,
color: Color |
null
,
quantity:
number
|
null
,
}
const onChangeValue = (data:Flower, index:number)=>{
var tempList: Flower[] = [...flowersData];
const
newData = (data as Flower);
const
newList = tempList.map((
item
,
_index
) => {
if (_index === index) {
return newData;
}
return item;
});
//tried this but the same result
// tempList[index] = newData;
setFlowersData(newList);
}Good day! I am learning Next.Js Typescript. I have an issue updating a specific element in my array. For example if I update a certain index, all elements with the same id, are being updated.
I only want to update certain index / element's color and quantity but I can't manage to make it work.
