I have an array of object named as options in the state using useState hook I just want to update a nested array object at a specific index.
var subOptionModel = {
text: "test",
price: 0,
};
var optionModel = {
optionSetId: 0,
optionName: "",
optionPrice: 0,
editOptionName: false,
subOptions: [subOptionModel],
};
const [options, setOptions] = useState([optionModel]);
I have multiple options in options state, how can I update the state like the option at index 2 and suboption at 1 here is what I tried so for.
setOptions(
options.map((x, index) => {
if (index !== optionIndex) return x;
x.subOptions.map((subItem, subIndex) => {
console.log(subItem);
if (subIndex !== subOptionIndex) return subItem;
return {
...subItem,
text: text
};
});
}),
);