如何设置词典列表,仅更新一部分

时间:2019-11-23 04:38:28

标签: javascript reactjs dictionary

我要设置以下词典列表:

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]

0 个答案:

没有答案