简体   繁体   English

为什么这些返回数组的方式在Javascript中有所不同?

[英]Why do these ways of returning arrays differ in Javascript?

I have two examples: 我有两个例子:

function myFunction(arr, size) {
  // Break it up.
 var newArray = [];

  var tempArray = arr.splice(0,1);
 newArray.push(arr.splice(0,1));
 console.log(newArray);

  return arr;
}

myFunction(["a", "b", "c", "d"], 2);

And this one: 还有这个:

function myFunction(arr, size) {
  // Break it up.
 var newArray = [];

  var tempArray = arr.splice(0,1);
 newArray.push(tempArray);
 console.log(newArray);

  return arr;
}

myFunction(["a", "b", "c", "d"], 2);

The main difference being the line: 主要区别在于:

 newArray.push(arr.splice(0,1)); // Or newArray.push(tempArray);

Why does the first example return Array[1] and the second example return ["a"]? 为什么第一个示例返回Array [1],第二个示例为何返回[“ a”]?

I was expecting to get ["a"] regardless of which way I went, can someone possibly help me understand what is happening here? 无论我走哪条路,我都希望得到[“ a”],有人可以帮助我了解这里发生了什么吗?

I was just trying to take the first element of the array by splicing (which I believe returns an array of the removed elements) and push this array onto my "newArray" so I can ultimately have an array containing nested arrays holding each character. 我只是想通过拼接来获取数组的第一个元素(我相信该元素会返回已删除元素的数组),然后将该数组推入我的“ newArray”,以便最终得到一个包含嵌套数组的数组,其中每个数组都包含嵌套数组。 Ie [["a"],["b"],["c"]...] 即[[“ a”],[“ b”],[“ c”] ...]

EDIT: Please ignore the "size" parameter. 编辑:请忽略“大小”参数。 EDIT2: Sincere apologies. EDIT2:真诚的道歉。 I'm not worried about the return statement. 我不担心退货声明。 Stupid of me to forgot to mention. 我笨到忘了提。 I'm looking at my console.log output. 我正在查看我的console.log输出。 When I run the script and look at the console, that's when I'm getting Array[1] or ["a"]. 当我运行脚本并查看控制台时,就是在获取Array [1]或[“ a”]的时候。

This is the most important information you need to have in mind: splice changes the original array (read this: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice ). 这是您需要记住的最重要的信息: splice更改了原始数组(请阅读: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice )。

So, let's see your first function. 因此,让我们看看您的第一个功能。 This line: 这行:

var tempArray = arr.splice(0,1);

Changes arr , as expected. 按预期更改arr But, when you do this in the next line: 但是,当您在下一行中执行此操作时:

newArray.push(arr.splice(0,1));

You are changing arr again! 您再次更改了arr

Your second function, on the other hand, changes arr just once: 另一方面,您的第二个功能仅更改arr一次:

var tempArray = arr.splice(0,1);
newArray.push(tempArray);

In a nutshell: just count how many times you have a splice in your first function (2 times, changing the original array twice) and how many times you have a splice in your second function (1 time, changing the original array only once). 简而言之:只需计算您在第一个函数中有一个splice次数(2次,将原始数组更改两次)和在第二个函数中有一个splice次数(1次,仅更改一次原始数组) 。

EDIT: regarding the console.log, newArray is an array with an array, and that's why sometimes you see Array[1] . 编辑:关于console.log, newArray是带有数组的数组,这就是为什么有时您会看到Array[1] Do this: 做这个:

console.log(newArray[0]);

And now you'll see ["a"] , ["b"] or whatever. 现在,您会看到["a"]["b"]或其他内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM