JavaScript:'shift'方法背后的逻辑是什么?

时间:2022-12-12 21:27:17

Basically, how do you return AND remove the first element of an array WITHOUT using the shift() OR splice() methods(or any other methods for that matter)?

基本上,如何返回并删除数组的第一个元素而不使用shift()或splice()方法(或任何其他方法)?

thanks in advance!

提前致谢!

2 个解决方案

#1


5  

What is the logic behind the 'shift' method?

“转变”方法背后的逻辑是什么?

It's fully described in the specification.

它在规范中有详细描述。

Basically, how do you return AND remove the first element of an array WITHOUT using the shift() OR splice() methods(or any other methods for that matter)?

基本上,如何返回并删除数组的第一个元素而不使用shift()或splice()方法(或任何其他方法)?

I can't see any good reason for such a restriction, but you can use a loop to manually assign each entry the value of the one above it (starting at the end) until the index you want to change, and then set the length of the array to one less. Something vaguely like this (I'm sure this is not a complete implementation of what shift does):

我看不出有这样一个限制的任何好理由,但你可以使用一个循环手动为每个条目分配它上面的一个值(从结尾开始)直到你想要改变的索引,然后设置长度数组少了一个。有点像这样的东西(我确信这不是一个完整的实现什么转移):

Live example | Live source

实例|直播源

var n;
for (n = theArray.length - 2; n >= removeAt; --n) {
    theArray[n] = theArray[n+1];
}
--theArray.length;

Altering the length property of an array will remove any elements that no longer fall within the length.

更改数组的length属性将删除任何不再长度的元素。

You can also remove an element without altering the length of the array, using delete:

您还可以使用delete删除元素而不更改数组的长度:

delete theArray[removeAt];

The array's length will be unchanged, but it will no longer have an entry (at all) at that location. It becomes sparse (if it wasn't already). This works because the delete statement removes properties from objects, and untyped JavaScript arrays are really just objects.

数组的长度将保持不变,但在该位置将不再有条目(根本没有)。它变得稀疏(如果它还没有)。这是有效的,因为delete语句从对象中删除属性,而无类型的JavaScript数组实际上只是对象。

#2


0  

I think this is what you are looking for. It sets the first letter, then adds-on the rest of the string, replaces the first letter in the finalString variable with an empty string and then returns the first letter saved in the firstLetter variable.

我想这就是你要找的东西。它设置第一个字母,然后添加字符串的其余部分,用空字符串替换finalString变量中的第一个字母,然后返回保存在firstLetter变量中的第一个字母。

function remove(str){
  let firstLetter = '';
  let finalString = '';
  for(let i = 0; i < str.length; i++){
    if(i === 0){
      firstLetter = str[i];
    }
    finalString += str[i];
    if(i === 0){
      finalString = '';
    }
  }
  return firstLetter;
}

remove('hello');

#1


5  

What is the logic behind the 'shift' method?

“转变”方法背后的逻辑是什么?

It's fully described in the specification.

它在规范中有详细描述。

Basically, how do you return AND remove the first element of an array WITHOUT using the shift() OR splice() methods(or any other methods for that matter)?

基本上,如何返回并删除数组的第一个元素而不使用shift()或splice()方法(或任何其他方法)?

I can't see any good reason for such a restriction, but you can use a loop to manually assign each entry the value of the one above it (starting at the end) until the index you want to change, and then set the length of the array to one less. Something vaguely like this (I'm sure this is not a complete implementation of what shift does):

我看不出有这样一个限制的任何好理由,但你可以使用一个循环手动为每个条目分配它上面的一个值(从结尾开始)直到你想要改变的索引,然后设置长度数组少了一个。有点像这样的东西(我确信这不是一个完整的实现什么转移):

Live example | Live source

实例|直播源

var n;
for (n = theArray.length - 2; n >= removeAt; --n) {
    theArray[n] = theArray[n+1];
}
--theArray.length;

Altering the length property of an array will remove any elements that no longer fall within the length.

更改数组的length属性将删除任何不再长度的元素。

You can also remove an element without altering the length of the array, using delete:

您还可以使用delete删除元素而不更改数组的长度:

delete theArray[removeAt];

The array's length will be unchanged, but it will no longer have an entry (at all) at that location. It becomes sparse (if it wasn't already). This works because the delete statement removes properties from objects, and untyped JavaScript arrays are really just objects.

数组的长度将保持不变,但在该位置将不再有条目(根本没有)。它变得稀疏(如果它还没有)。这是有效的,因为delete语句从对象中删除属性,而无类型的JavaScript数组实际上只是对象。

#2


0  

I think this is what you are looking for. It sets the first letter, then adds-on the rest of the string, replaces the first letter in the finalString variable with an empty string and then returns the first letter saved in the firstLetter variable.

我想这就是你要找的东西。它设置第一个字母,然后添加字符串的其余部分,用空字符串替换finalString变量中的第一个字母,然后返回保存在firstLetter变量中的第一个字母。

function remove(str){
  let firstLetter = '';
  let finalString = '';
  for(let i = 0; i < str.length; i++){
    if(i === 0){
      firstLetter = str[i];
    }
    finalString += str[i];
    if(i === 0){
      finalString = '';
    }
  }
  return firstLetter;
}

remove('hello');