[英]Remove item from array in JavaScript
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 statement
说,如果我的索引为3,则大于3,然后在index
拼接并删除一个。
selectedData.push(TheThing);
I then push TheThing
to my array if the if statement above isn't true. 如果上面的if语句不正确,则将TheThing
推TheThing
我的数组中。
However, I have a variable var arrayLength = selectedData.length;
但是,我有一个变量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. 它抓住了长度,当我进行控制台记录时,它从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
检查我的数组并删除添加到我的数组中的第三个数组之后的所有内容。
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. 没有抓住第三个索引-它抓住看到3的第一个索引,如果没有,则抓住-1。 Replace your if statement with, 将if语句替换为
if (selectedData.length > 3) {
selectedData.pop() // removes last element (stack)
// or
selectedData = selectedData.slice(1) //remove first element (queue)
}
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索引。 ` `
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()可以接受许多参数,并且您也应该处理这种情况
var index = selectedData.indexOf(3); var index = selectedData.indexOf(3); simply give you the index of the element of the array that has value 3 Example 只需给您具有值3的数组元素的索引即可
selectedData = [ 0, 3 , 2];
alert( selectedData.indexOf( 3 ) ); // this will alert "1" that is the index of the element with value "3"
you can use this scenario 你可以使用这种情况
var selectedData = [];
myElement.on('click', function() {
//if selectedData length is less than 3, push items
});
This could work. 这可能有效。
myElement.on('click', function() {
if(selectedData.length > 3){
selectedData = selectedData.splice(0, 3);
}
console.log(selectedData.length, 'the length');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.