我要设置以下词典列表:
x = [ {'a': 100, 'n':300}, {'a': 10, 'n':30}, {'a': 160, 'n':300}, {'a': 94, 'n': 346}]
这样:
当我更新时:x[3]['n'] = 4034
我实际上想这样做
x = [ ...x, x[3]['n']=4034]
获得:
x = [ {'a': 100, 'n':300}, {'a': 10, 'n':30}, {'a': 160, 'n':300}, {'a': 94, 'n': 4034}]
这是由于ReactJS useState:
我在reactJs中使用它,并想实现它的useState,其中const [x, setx] = useState(the list of dictionaries)
,所以当我调用setx(x => x = [...x, updated_part]