简体   繁体   English

在给定索引 n 处将 arr1 复制到 arr 2,并且在 function 运行后 arr 1 和 arr2 应该相同

[英]copy arr1 to arr 2 at given index n and arr 1 and arr2 should be same after function runs

I am trying to copy arr1 to arr 2 at given index n and arr 1 and arr2 should be same after function runs.我正在尝试在给定索引 n 处将 arr1 复制到 arr 2,并且在 function 运行后 arr 1 和 arr2 应该相同。 So I have put a = arr2 and operate on a but arr2 is also changing所以我放了 a = arr2 并对 a 进行操作,但 arr2 也在变化

 function frankenSplice(arr1, arr2, n) { let a = arr2 console.log(a) a.splice(n,0,...arr1); console.log(arr2) return a } console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));

Arrays in javascript are mutable, so when you use javascript 中的 Arrays 是可变的,所以当你使用

let a = arr2

array a becomes arr2 , and when you modify one, you modify both (beacuse they are essentialy the same array)数组a变为arr2 ,当你修改一个时,你同时修改了两个(因为它们本质上是同一个数组)

To solve your problem, you can use spread operator to copy content of your original array to the new one, so it would look like this为了解决您的问题,您可以使用扩展运算符将原始数组的内容复制到新数组,所以它看起来像这样

let a = [...arr2]

You need to make a a copy of arr2 , one way to do that would be to use slice :您需要制作arr2 a副本,一种方法是使用slice

 const frankenSplice = (arr1, arr2, n) => { const a = arr2.slice(0); // 0 is not required but makes it slightly faster a.splice(n, 0, ...arr1); return a } const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; console.log(frankenSplice(arr1, arr2, 1)); console.log(arr1); console.log(arr2);

Using flatMap should make simplify for this case.使用flatMap应该可以简化这种情况。

 function frankenSplice(arr1, arr2, n) { return arr2.flatMap((num, i) => (i === n? [...arr1, num]: num)); } console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试编写 function 其中 arr1[0] = arr2[0], arr1[1] = arr2[1], ... 等等。 我该怎么做呢? - I am trying to write a function where arr1[0] = arr2[0], arr1[1] = arr2[1], … and so on. How do I do this? arr1.concat(arr2)和[] .concat(arr1,arr2)之间的区别 - Difference Between arr1.concat(arr2) And [].concat(arr1, arr2) 如何从arr1和arr2获取结果,当ID匹配时我需要从arr1复制内容 - How can I get the result from arr1 and arr2, When the ID matches I need to copy the content from arr1 如何连接arr1数组和arr2数组(基于arr2数组结构) - How to join arr1 array and arr2 array (based on arr2 array structure) 是否有一个loadash函数可以比较两个数组并仅在arr1中存在来自arr2的所有值时才返回true? - Is there a loadash function to compare two arrays and return true only if all values from arr2 is present in arr1? 将getIntersect(arr1,arr2)Javascript函数转换为任意数量的参数并在JQuery中 - Convert getIntersect(arr1, arr2) Javascript function to any number of arguments and in JQuery 制作一个接收两个 arrays 的 function。 将 arr1 和 arr2 中的所有数字相加。 如果 arr1 的总和等于 arr2 返回 true。 如果不是,则为假 - make a function that takes in two arrays. Add all the numbers in arr1 & arr2. If the sum of arr1 is equal to arr2 return true. False if not 如何在 JavaScript 中将数组 (arr1) 的值推送到新的空数组 (arr2) 中? - How to push a value of an array (arr1) into a new empty array (arr2) in JavaScript? 过滤 arr1 > obj1 > arr2 > obj2 中的多个第一个对象 - filter multiple first objects in arr1 > obj1 > arr2 > obj2 a[arr1[i]]=true 有什么作用? - What does a[arr1[i]]=true do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM