简体   繁体   English

将第一个数组的元素复制到第二个没有双重com的元素

[英]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.

相关问题 如何在不覆盖第一个元素的情况下向数组添加新元素? - How to add new elements to an array without overwriting the first one? 使用一个提交发送双表单,同时将电子邮件字段从第一个表单拉到第二个表单 - Sending Dual Forms with one submit, while pulling the email field from first Form into the second 通过 map () function 复制没有第一个 object 的数组 - copying an array without the first object via the map () function Javascript在另一个第二个数组的位置迭代第一个数组的元素 - Javascript Iterate elements of a first array at the position of another second array 将第二个数组的元素随机分布在第一个数组中 - distribute the elements of second array inside the first array randomly 将元素从二维数组复制到另一个数组 - Copying elements from 2D array into another one 为什么第一个给我“,”而第二个没有“,”? - why the first one gives me “,” while the second one is without “,”? javascript 用第二个数组值替换第一个数组值。 第一个数组长度为 2,第二个数组长度为 7 - javascript replace first array value with second array value. First array length is 2 and second one length is 7 javascript算法-返回第一个数组中第二个不存在的元素 - javascript algorithm - returning elements in first array which are not present in second 根据一个数组渲染复选框,并根据第二个数组选中元素 - Rendering checkboxes based on one array, and checked elements based on the second array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM