为了在JavaScript中复制数组:以下哪个更快使用?
var dup_array = original_array.slice();
Run Code Online (Sandbox Code Playgroud)
For 环for(var i = 0, len = original_array.length; i < len; ++i)
dup_array[i] = original_array[i];
Run Code Online (Sandbox Code Playgroud)
我知道两种方式只做一个浅的副本:如果original_array包含对象的引用,则不会克隆对象,但只会复制引用,因此两个数组都将引用相同的对象.但这不是这个问题的重点.
我只询问速度.
我在这里读到了很多关于"按值"和"按引用"传递以将数组发送到javascript函数的答案.然而,我在向函数发送数组并使原始数组保持不变时遇到问题.这个例子说明了这个问题:
function myFunction(someArray)
{
// any function that makes an array based on a passed array;
// someArray has two dimensions;
// I've tried copying the passed array to a new array like this (I've also used 'someArray' directly in the code);
funcArray = new Array();
funcArray = someArray;
var i = 0;
for(i=0; i<funcArray.length; i++)
{
funcArray[i].reverse;
}
return funcArray;
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么这个函数中的任何东西都应该改变原始数组.
如果将函数调用分配给新数组,则调用此函数会直接更改原始数组:
myArray = [["A","B","C"],["D","E","F"],["G","H","I"]];
anotherArray = new Array();
anotherArray = myFunction(myArray);
// myArray gets modified!;
Run Code Online (Sandbox Code Playgroud)
我尝试使用.valueOf()发送原语:
anotherArray …Run Code Online (Sandbox Code Playgroud) 如何在Javascript中将项目数组向上移动4个位置?
我有以下字符串数组:
var array1 = ["t0","t1","t2","t3","t4","t5"];
Run Code Online (Sandbox Code Playgroud)
我需要一个函数转换"array1"导致:
// Note how "t0" moves to the fourth position for example
var array2 = ["t3","t4","t5","t0","t1","t2"];
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我有这个数组:
var arr1 = ['a1', 'a2', 'a3', 'a4', 'a5'];
Run Code Online (Sandbox Code Playgroud)
我需要通过两个位置将它移到右边,比如说
arr1 = ['a4', 'a5', 'a1', 'a2', 'a3'];
Run Code Online (Sandbox Code Playgroud)
这适用于左移:
arr1 = arr1.concat(arr1.splice(0,2)); // shift left by 2
Run Code Online (Sandbox Code Playgroud)
我明白了:
arr1 = ['a3', 'a4', 'a5', 'a1', 'a2'];
Run Code Online (Sandbox Code Playgroud)
但是我不知道怎么做右移......
我正在研究 Hackerrank 上的数组左旋转。我拥有的解决方案将 console.log 包含正确结果的数组,但不会使用 return 工作。这是他们网站的详细信息 - “打印一行 n 个空格分隔的整数,表示执行 d 次左旋转后数组的最终状态。” 我已经读到问题可能与在 node.js 中运行的异步函数有关,但我不确定如何解决这个问题。
// sample input - 1 2 3 4 5
// sample output - 5 1 2 3 4
function rotLeft(a, d) {
var arr = [];
for (var i = 1; i <= a; i++){
arr.push(i)
};
for (var j = 1; j <= d; j++){
arr.shift(arr.push(j))
}
console.log(arr.toString()); // <-- this will print the desired output.
return arr.toString(); // <-- no return from …Run Code Online (Sandbox Code Playgroud)如果我有两个数组都包含10000个项目,那么现在我想将它们合并为一个数组,所以我用concat做到了:
array1=array1.concat(array2);
Run Code Online (Sandbox Code Playgroud)
但是有人知道什么时间吗?常数还是N?有没有更好的方法来合并它们的速度。感谢您的任何提示。
我正在编写一个Snake游戏
蛇的运动的逻辑决定了如果我有一个Javascript数组
var links = [elem_0, elem_1, ..., elem_n];
Run Code Online (Sandbox Code Playgroud)
代表蛇的链接元素,那么对于蛇的移动方式是蹦出来elem_n,改变它的位置是在中elem_0加翻译单元dx和dy,然后把它放在数组的开始:
[elem_0, elem_1, ..., elem_n] ---> [elem_n, elem_0, ..., elem_(n-1)]
(具有一些内部属性 elem_n在此过程更改)
这样做的方法是什么,不做任何妥协
????
我有一系列代表虚拟轮播的项目.
const carousel = ['a','b','c','d','e'];
let currentIndex = 0;
function move (amount) {
const l = items.length; // in case carousel size changes
// need to update currentIndex
return carousel[currentIndex];
}
Run Code Online (Sandbox Code Playgroud)
什么是干净或聪明的方式来处理向左currentIndex == 0移动和向右移动时currentIndex == length-1?
我之前已经考虑过这个问题,从未有过任何非常聪明或简洁的事情.
大家好,我有这个任务:我有一个数组 [4,7,3,6,9] ,我必须制作一个这样的数组:
[4,7,3,6,9]
[9,4,7,3,6]
[6,9,4,7,3]
[3,6,9,4,7]
[7,3,6,9,4]
Run Code Online (Sandbox Code Playgroud)
我必须制作一个程序,其中数组正在旋转,即使我向数组添加一个新项目,它也应该相应地改变。我是 JS 的新手,大约 1 周,这是我目前的尝试:
var numbers = [4, 7, 3, 6, 9];
console.log(numbers);
numbers[0] = 9; numbers[1] = 4; numbers[2] = 7; numbers[3] = 3; numbers[4] = 6;
console.log(numbers);
numbers[0] = 6; numbers[1] = 9; numbers[2] = 4; numbers[3] = 7; numbers[4] = 3;
console.log(numbers);
numbers[0] = 3; numbers[1] = 6; numbers[2] = 9; numbers[3] = 4; numbers[4] = 7;
console.log(numbers);
numbers[0] = 7; numbers[1] = 3; numbers[2] = 6; numbers[3] …Run Code Online (Sandbox Code Playgroud) 我有一个阵列,
var myArray = [ 1,2,3,4,5 ]
Run Code Online (Sandbox Code Playgroud)
和变数,
var count = 5
Run Code Online (Sandbox Code Playgroud)
伪代码:
if count = 1, output myArray = [5,1,2,3,4]
if count = 2, then myArray = [ 4,5,1,2,3]
Run Code Online (Sandbox Code Playgroud)
等等 ..
如何在不使用循环的情况下实现此目的?
javascript ×11
arrays ×8
algorithm ×2
copy ×1
duplicates ×1
game-physics ×1
numbers ×1
optimization ×1
push ×1
rotation ×1
shift ×1
slice ×1