首页 > 解决方案 > 如何更改对象数组的状态。使用它们的数组位置

问题描述

例如我想改变状态route[2]--> address --> apt_plot 但是我面临的问题是我怎样才能像 setRoute([2]) 有人可以帮助那样访问位置 2。下面的示例数据。将此作为解决方案的参考。

const [route, setRoute] = useState([
        {
          address : {
            apt_plot: '',
            street: '',
            city: '',
            state: '',
            zip_code: '',
          },
          geo_location : {
            latitude: '',
            longitude: '',
          }
       },
       {
          address : {
            apt_plot: '',
            street: '',
            city: '',
            state: '',
            zip_code: '',
          },
          geo_location : {
            latitude: '',
            longitude: '',
          }
       },
       {
          address : {
            apt_plot: '',
            street: '',
            city: '',
            state: '',
            zip_code: '',
          },
          geo_location : {
            latitude: '',
            longitude: '',
          }
       }
])

标签: reactjsreact-hooksstatesetstate

解决方案


推荐阅读