我使用的數據集的字段爲continent
,country
,city
和`street。如你所想,每個領域取決於前一個領域。如何在值更改時清空對象的所有後續字段?
現在假定存在(部分)已完成的數據集,並且用戶更改country
值。然後所有以下字段必須是空的。
這就是我如何做的(在我的情況下,數據是作爲一個狀態存儲在一個反應組件中 - 但這不應該在這裏)。 但對我來說,這看起來非常不活潑。我希望這可以做得更多'程序性'。
我的問題是,我不能只是迭代對象,因爲字段的順序不固定在一個js對象中。這將是一個陣列,但這裏並不是這樣...
if (data.name === 'continent') {
this.setState({
continent: undefined,
country: undefined,
city: undefined,
street: undefined
})
} else if (data.name === 'country') {
this.setState({
country: undefined,
city: undefined,
street: undefined
})
} else if (data.name === 'city') {
this.setState({
city: undefined,
street: undefined
})
} else if (data.name === 'street') {
this.setState({
street: undefined
})
}
但是,這將改變只有一個單一的價值......吧? – user3142695
編號所有適用的。這是一個直通開關 – Amit
可能更好,只需在對象上設置道具,然後在開關後設置一次狀態,而不是幾次 – mhodges