[英]Copying elements of first array to second one without dual com
I've got some problems with these two arrays. 我对这两个数组有一些问题。
for (i = 0; i < post_data_route.length; i++) {
route_array.push(coordinates_array[post_data_route[i] - 1]);
}
route_array[route_array.length - 1][0] -= 0.00001;
route_array[route_array.length - 1][1] -= 0.00001;
route_array[route_array.length - 1]
equals route_array[0]
but when I try to change one of them second one changes too. route_array[route_array.length - 1]
等于route_array[0]
但是当我尝试更改其中一个时,第二个也会改变。 I guess that's about the pointer but why does it happen? 我想这是关于指针,但为什么会发生? Isn't it just pushing values of array to another one?
是不是只是将数组的值推向另一个? How could I get rid of it?
我怎么能摆脱它? Thanks for your answers and suggestions.
感谢您的回答和建议。
You are pushing not the values, but another array, because your array is multidimensional. 您推送的不是值,而是另一个数组,因为您的数组是多维的。 You need to push the value of each dimensions separately.
您需要分别推送每个维度的值。
Example: 例:
route_array.push([
coordinates_array[post_data_route[i] - 1][0],
coordinates_array[post_data_route[i] - 1][1]
]);
You could use Array#slice
for the data, that means, you get a copy. 您可以使用
Array#slice
作为数据,这意味着您可以获得副本。
The
slice()
method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included).slice()
方法将数组的一部分的浅表副本返回到从头到尾选择的新数组对象(不包括结束)。 The original array will not be modified.原始数组不会被修改。
for (i = 0; i < post_data_route.length; i++) {
route_array.push(coordinates_array[post_data_route[i] - 1].slice());
}
you can create new objects and arrays with JSON.parse(JSON.stringify(yourArray))
你可以使用
JSON.parse(JSON.stringify(yourArray))
创建新对象和数组JSON.parse(JSON.stringify(yourArray))
its not so nice but its working 它不太好但是它的工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.