admin管理员组文章数量:1355658
In C# I would create a List then I could add and remove numbers very easily. Does identical functionality exist in Javascript, or do I have to write my own methods to search and remove items using a loop?
var NumberList = [];
NumberList.Add(17);
NumberList.Add(25);
NumberList.Remove(17);
etc.
I know I can use .push to add a number, so I guess it's really how to remove an individual number without using a loop that I'm looking for.
edit: of course, if there's no other way then I'll use a loop!:)
In C# I would create a List then I could add and remove numbers very easily. Does identical functionality exist in Javascript, or do I have to write my own methods to search and remove items using a loop?
var NumberList = [];
NumberList.Add(17);
NumberList.Add(25);
NumberList.Remove(17);
etc.
I know I can use .push to add a number, so I guess it's really how to remove an individual number without using a loop that I'm looking for.
edit: of course, if there's no other way then I'll use a loop!:)
Share Improve this question edited Mar 11, 2011 at 16:07 NibblyPig asked Mar 11, 2011 at 15:58 NibblyPigNibblyPig 53k75 gold badges219 silver badges380 bronze badges7 Answers
Reset to default 3The Array objet has this kind of method :
var myArray = new Array();
myArray.push(12);
myArray.push(10);
myArray.pop();
All detail can be found here
To remove a specific value , some tricks are possible :
var id = myArray.indexOf(10); // Find the index
if(id!=-1) myArray.splice(id, 1);
You have to use splice and indexOf if you know that there is only one copy of the value that you want to remove and if there can be many copies then you have to use splice in a loop.
If you're using Underscore.js then you can use:
array = _.without(array, 17);
To remove array element by value :
Array.prototype.removeByValue = function(val) {
for(var i=0; i<this.length; i++) {
if(this[i] == val) {
this.splice(i, 1);
break;
}
}
}
var myarray = ["one", "two", "three", "four", "five"];
myarray.removeByValue("three");
console.log(myarray); // ["one", "two", "four", "five"];
or in your case an array of numbers:
var myarray = [1, 2, 3, 4, 5];
myarray.removeByValue(3);
console.log(myarray); // [1, 2, 4, 5];
to remove by index you'll have to use splice():
myarray.splice(2,1); //position at 2nd element and remove 1 element
console.log(myarray); // ["one", "two", "four", "five"];
var NumberList = {};
NumberList[17] = true;
NumberList[25] = true;
delete NumberList[17];
This uses the "associative array"-characteristic of JavaScript objects to let you store and retrieve values by index in an object.
I used true
as the value, but you can use anything you like, as it is not important (at least as per your example). You could of course store more useful things there. Using true
has the nice side-effect that you can do an existence-check like this:
if (NumberList[25]) // evaluates to "true"
if (NumberList[26]) // evaluates to "undefined" (equivalent to "false" here)
The same would work with actual array objects, by the way.
var NumberList = [];
NumberList[17] = true;
NumberList[25] = true;
delete NumberList[17];
but these would not be "sparse" - NumberList[25] = true
creates an 26-element array with all preceding array elements set to undefined
.
Using objects instead is sparse, no additional members are created.
If you want in-place removal, then indexOf
and splice
can be used together. To remove all occurrences of 17, use
var index;
while((index = NumberList.indexOf(17)) != -1) {
NumberList.splice(index, 1);
}
If you don't care about in-place removals, then the filter
method can be used.
NumberList = NumberList.filter(function(number) {
return number != 17;
});
You could store the index of each added element (number). And then use splice to remove by index. John has a good array remove function to remove by index.
Something like:
var array = [];
var number = { val: 10, index: null };
// add to array
array.push(number.val);
number.index = array.length - 1;
// remove (using John's remove function)
array.remove(number.index);
// remove using splice
array.splice(number.index, 1);
I've solved it by using the JQuery function inArray();
bined with splice()
.
indexOf
and inArray
seem to be identical, however indexOf
isn't supported in IE6 or 7 as it turns out, so I had to either write my own or use JQuery, and I use JQuery anyway.
本文标签:
版权声明:本文标题:What's an easy way of storing an array of numbers in Javascript that I can addremove from? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743960480a2568931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论