If one were to look at Java class Vector in the official API, there are certain conveniences such as creating a Vector object without specifying its initial length.
One can just add elements to it without having to specify the index (like an array).
One can also use .contains to determine if the Vector collection contains the element, without having to loop.
Is there such a type in Javascript?
If one were to look at Java class Vector in the official API, there are certain conveniences such as creating a Vector object without specifying its initial length.
One can just add elements to it without having to specify the index (like an array).
One can also use .contains to determine if the Vector collection contains the element, without having to loop.
Is there such a type in Javascript?
Share Improve this question asked Oct 23, 2013 at 19:16 LaneLaneLaneLane 3537 silver badges16 bronze badges3 Answers
Reset to default 3JavaScript arrays don't have fixed lengths and are essentially equivalent to Vector.
- You can add an element at any index. The
.length
property keeps track of the biggest numerically-indexed property. - The
.indexOf
method is like.contains()
. (Don't make heavy use of this, either in Java or JavaScript. Version 6 of Ant, for example, had horrible performance problems in large projects because code was written to more-or-less use ArrayList instances like Maps. If you have collections of any size and need to do look-ups frequently, use a better data structure.) To add an element to the end, you can do either:
someArray.push( newValue );
or:
someArray[someArray.length] = newValue;
You can create Array
and not specify its size:
var array = [];
You can push to it w/o index:
array.push(value);
You can check if it contains specific value, like so:
array.indexOf(value); // returns -1 if it didn't find a match
Check out the spec if you need to find more info about arrays in JavaScript (and only about them).
What you're after is basically a simple JS array. You can use push()
to add to the array without having to worry about indexes. indexOf()
operates like contains().
Have a look at:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745137902a4613290.html
评论列表(0条)