在JavaScript中从数组中删除项目

时间:2022-05-27 04:04:33

Seen this question a lot, but cannot find something that's what i'm looking for.

很多人都看过这个问题,但找不到我正在寻找的东西。

onClick I push an item to an array I have, however, if there's 3 items in my array I don't want to be able to push items anymore.

onClick我将项目推送到我拥有的数组,但是,如果我的数组中有3个项目我不想再推送项目了。

var selectedData = [];

I set my empty variable.

我设置了我的空变量。

var index = selectedData.indexOf(3);

I then get the index of my array which is 3

然后我得到我的数组的索引是3

if (index > 3) {
  selectedData.splice(index, 1);
}

Then within my if statement I say, if my index which is 3, is bigger then 3, then splice at index and remove one.

然后在我的if语句中,我说,如果我的索引是3,那么大于3,那么拼接索引并删除一个。

selectedData.push(TheThing);

I then push TheThing to my array if the if statement above isn't true.

然后,如果上面的if语句不为真,我将TheThing推送到我的数组。

However, I have a variable var arrayLength = selectedData.length; that grabs the length, and when I console log it, it starts at 0 and splices items anything after 4. Not 3.

但是,我有一个变量var arrayLength = selectedData.length;抓取长度,当我控制台记录它时,它从0开始,拼接项目后的任何东西4.不是3。

Any idea what i've done wrong or misunderstood? Thanks

知道我做错了什么或被误解了吗?谢谢


More full example of my code

更完整的代码示例

var selectedData = [];

myElement.on('click', function() {

  var index = selectedData.indexOf(3);
      if (index > 3) {
          selectedData.splice(index, 1);
      }
  var arrayLength = selectedData.length;

  console.log(arrayLength, 'the length');

});

So in short, onClick check my array and remove anything after the third that gets added into my array.

所以简而言之,onClick检查我的数组并在第三个添加到我的数组后删除任何内容。

6 个解决方案

#1


1  

Do you want this to behave as a stack or a queue?

您希望它表现为堆栈还是队列?

So your code here:

所以你的代码在这里:

var index = selectedData.indexOf(3);

Is not grabbing the 3rd index - its grabbing the first index where it sees 3, or -1 if it doesn't. Replace your if statement with,

不是抓住第三个索引 - 它抓住了它看到3的第一个索引,如果没有,则抓取-1。将if语句替换为,

if (selectedData.length > 3) {
    selectedData.pop() // removes last element (stack)
    // or
    selectedData = selectedData.slice(1) //remove first element (queue)
}

#2


0  

I think you need to try var arrayLength = selectedData.length -1;

我想你需要尝试var arrayLength = selectedData.length -1;

You start at 0 like a normal array, but don't you start with an empty array?

你像普通数组一样从0开始,但是不是以空数组开始吗?

Plus when you use .length, it returns the true count of the array or collection not a 0 index. `

另外,当您使用.length时,它返回数组或集合的真实计数而不是0索引。 `

#3


0  

you can override push() method of your array like this:

你可以覆盖你的数组的push()方法,如下所示:

var a = [];
a.push = function(){}

or like this

或者像这样

a.push = function (newEl){
  if(this.length <3){
    Array.prototype.push.call(this, newEl)
  }
}

This is not complete example because push() can take many arguments and you should to handle this case too

这不是完整的例子,因为push()可以采用许多参数,你也应该处理这种情况

#4


0  

var index = selectedData.indexOf(3); simply give you the index of the element of the array that has value 3 Example

var index = selectedData.indexOf(3);只需为您提供值为3的数组元素的索引

 selectedData = [ 0, 3 , 2];
 alert( selectedData.indexOf( 3 ) ); // this will alert "1" that is the index of the element with value "3"

#5


0  

you can use this scenario

你可以使用这个场景

var selectedData = [];

myElement.on('click', function() {

  //if selectedData length is less than 3, push items

});

#6


0  

This could work.

这可行。

myElement.on('click', function() {

    if(selectedData.length > 3){
        selectedData = selectedData.splice(0, 3);
    }

    console.log(selectedData.length, 'the length');

});

#1


1  

Do you want this to behave as a stack or a queue?

您希望它表现为堆栈还是队列?

So your code here:

所以你的代码在这里:

var index = selectedData.indexOf(3);

Is not grabbing the 3rd index - its grabbing the first index where it sees 3, or -1 if it doesn't. Replace your if statement with,

不是抓住第三个索引 - 它抓住了它看到3的第一个索引,如果没有,则抓取-1。将if语句替换为,

if (selectedData.length > 3) {
    selectedData.pop() // removes last element (stack)
    // or
    selectedData = selectedData.slice(1) //remove first element (queue)
}

#2


0  

I think you need to try var arrayLength = selectedData.length -1;

我想你需要尝试var arrayLength = selectedData.length -1;

You start at 0 like a normal array, but don't you start with an empty array?

你像普通数组一样从0开始,但是不是以空数组开始吗?

Plus when you use .length, it returns the true count of the array or collection not a 0 index. `

另外,当您使用.length时,它返回数组或集合的真实计数而不是0索引。 `

#3


0  

you can override push() method of your array like this:

你可以覆盖你的数组的push()方法,如下所示:

var a = [];
a.push = function(){}

or like this

或者像这样

a.push = function (newEl){
  if(this.length <3){
    Array.prototype.push.call(this, newEl)
  }
}

This is not complete example because push() can take many arguments and you should to handle this case too

这不是完整的例子,因为push()可以采用许多参数,你也应该处理这种情况

#4


0  

var index = selectedData.indexOf(3); simply give you the index of the element of the array that has value 3 Example

var index = selectedData.indexOf(3);只需为您提供值为3的数组元素的索引

 selectedData = [ 0, 3 , 2];
 alert( selectedData.indexOf( 3 ) ); // this will alert "1" that is the index of the element with value "3"

#5


0  

you can use this scenario

你可以使用这个场景

var selectedData = [];

myElement.on('click', function() {

  //if selectedData length is less than 3, push items

});

#6


0  

This could work.

这可行。

myElement.on('click', function() {

    if(selectedData.length > 3){
        selectedData = selectedData.splice(0, 3);
    }

    console.log(selectedData.length, 'the length');

});