Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

关于数组复制

[
    [{GOOD_NO: 1},{GOOD_NO: 1},{GOOD_NO: 1}],
    [{GOOD_NO: 2},{GOOD_NO: 2}],
    [],
    [{GOOD_NO: 4}]
]

想要复制数组中第二条数据,并添加到尾部,并且修改新添加的数据中的GOOD_NO为5,即:

[
    [{GOOD_NO: 1},{GOOD_NO: 1},{GOOD_NO: 1}],
    [{GOOD_NO: 2},{GOOD_NO: 2}],
    [],
    [{GOOD_NO: 4}],
    [{GOOD_NO: 5},{GOOD_NO: 5}],
]

可是只要遍历修改新添加的数据,被复制的那条数据也会跟着变,求代码求指点!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

复制的时候JSON.parse(JSON.stringify())一下,被复制的数据就不会变了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...