简体   繁体   English

Console.log输出错误结果[怪异行为]

[英]Console.log outputs false result [Weird Behaviour]

I have a string where I use split('') to convert into an array. 我有一个字符串,我使用split('')转换为数组。 I just want to delete the last item of an array using splice() . 我只想使用splice()删除数组的最后一项。

However, I get this weird result when I output the array after deleting the last item. 但是,在删除最后一项后输出数组时,我得到了这个奇怪的结果。 It outputs fine when I don't use console.log() in Chrome console. 当我console.log() in Chrome控制台中不使用console.log() in时输出正常。 Why is this happening? 为什么会这样?

(Try this code in the console. It outputs fine) (在控制台中尝试此代码。输出正常)

let strings = 'AA11111';
let splits = strings.split("");
splits.splice(0, splits.length - 1);

(This gives me the false result. [Outputs the last item of the array.]) (这给了我错误的结果。[输出数组的最后一项。])

 let strings = 'AA11111'; let splits = strings.split(""); splits.splice(0, splits.length - 1); console.log(splits); 

Array.prototype.splice() used to remove elements from an array and change the content of the original array and it take in the first parameter the index at which you need to delete elements and next parameter the number of elements to delete , so if you need to delete the last element you need to start at arr.length-1 and delete count should be 1 look at the example below Array.prototype.splice()用于从数组中删除元素并更改原始数组的内容,它在第一个参数中包含需要删除元素的索引,下一个参数包含要删除的元素数,所以如果你需要删除你需要在arr.length-1开始的最后一个元素,删除计数应该是1看下面的例子

 let strings = 'AA11111'; let splits = strings.split(""); splits.splice(splits.length - 1,1); console.log(splits); 

Here what happens is that all elements except the last one are removed . 这里发生的是除去最后一个元素之外的所有元素 And that is the reason why you get modified array with just 1 element when you console.log. 这就是为什么在console.log中只使用1个元素修改数组的原因。 And in the first case it is returning the deleted items . 在第一种情况下,它返回已删除的项目

splits.splice(0, splits.length - 1);

The first paramenter is the start position of the element to be deleted and second parameter is the number of elements 第一个参数是要删除的元素的起始位置,第二个参数是元素的数量

This output makes sense to me. 这个输出对我来说很有意义。

Reading from the Array.prototype#splice documentation. Array.prototype#splice文档中读取

Syntax 句法

array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Parameters 参数

start 开始

Index at which to start changing the array (with origin 0). 开始更改数组的索引(原点为0)。 If greater than the length of the array, actual starting index will be set to the length of the array. 如果大于数组的长度,实际的起始索引将设置为数组的长度。 If negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if absolute value is greater than the length of the array. 如果为负数,将从数组末尾开始那么多元素(带原点-1),如果绝对值大于数组长度,则将其设置为0。

deleteCount Optional deleteCount可选

An integer indicating the number of old array elements to remove. 一个整数,指示要删除的旧数组元素的数量。

You start at index 0, ('A'), you delete length-1 elements (six elements). 从索引0开始,('A'),删除长度为1的元素(六个元素)。 You're left with the last element. 你留下了最后一个元素。 ('1'). ( '1')。

If you want to delete the last element I would do this: 如果你想删除最后一个元素,我会这样做:

 let strings = 'AA11111'; let splits = strings.split(""); splits.splice(-1, 1); //starting at the last element, delete one element. console.log(splits); 

Here what happens is that all elements except the last one are removed. 这里发生的是除去最后一个元素之外的所有元素。 And that is the reason why you get modified array with just 1 element when you console.log. 这就是为什么在console.log中只使用1个元素修改数组的原因。 And in the first case it is returning the deleted items. 在第一种情况下,它返回已删除的项目。

 splits.splice(0, splits.length - 1); 
  The modified array with just 1 element when you console.log. And in the first case it is returning the deleted items. 

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

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