key and value inside the array which is in the state object
this.state = { frequency: { days: [], startdate: "", customdate: "" }, };
how could i add the key ad value inside the days array?
You can modify your state by using setState
like this:
this.setState({frequency: {...this.state.frequency, days: [...this.state.frequency.days, day]}});
function addKeyValue(key, value) {
this.setState(state => ({
...state,
frequency: {
...state.frequency,
days: [...state.frequency.days, {[key]: value}]
}
})
}
--Edit
Removing the key is a bit trickier.
function removeKeyValue(key, value) {
this.setState(state => {
const days = state.frequency.days;
const dayIndex = days.findIndex(pr => pr[key] === value);
const day = {...days[dayIndex]};
delete day[key];
return {
...state,
frequency: {
...state.frequency,
days: [...days.slice(0, dayIndex),
day,
...days.slice(dayIndex + 1)]
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.