If I have an array in Javascript that has been filled sporadically, how do I determine the number of non-nothing elements in the array?
for example:
var zipCodes = [];
zipCodes[10006] = 'New York, NY';
zipCodes[90210] = 'Los Angeles, CA';
zipCodes.length returns 90,211 here and I really don't want to loop through 90,209 nothings to find out I have two valid elements. Using a for-in loop means dealing with anything that might bubble up the prototype chain and trying to figure out if it's part of this array.
Is there any way, given the array defined above, I can extract "2" as the number of defined elements?
If I have an array in Javascript that has been filled sporadically, how do I determine the number of non-nothing elements in the array?
for example:
var zipCodes = [];
zipCodes[10006] = 'New York, NY';
zipCodes[90210] = 'Los Angeles, CA';
zipCodes.length returns 90,211 here and I really don't want to loop through 90,209 nothings to find out I have two valid elements. Using a for-in loop means dealing with anything that might bubble up the prototype chain and trying to figure out if it's part of this array.
Is there any way, given the array defined above, I can extract "2" as the number of defined elements?
Share Improve this question edited Dec 15, 2020 at 9:17 Alex 1,5801 gold badge15 silver badges27 bronze badges asked Feb 3, 2009 at 17:04 Yes - that Jake.Yes - that Jake. 17.1k15 gold badges72 silver badges99 bronze badges3 Answers
Reset to default 12You would need an object with key-value pairs (kind of like an associative array / hashtable) instead of an array:
var zipCodes = {};
zipCodes[10006] = 'New York, NY';
zipCodes[90210] = 'Los Angeles, CA';
for(var zipCode in zipCodes) {
if(zipCodes.hasOwnProperty(zipCode)) {//makes sure prototypes aren't taken into account
}
}
edit: or you can store objects in an array like this:
var zipCodes = [];
zipCodes.push({10006:'New York, NY'});
zipCodes.push({90210: 'Los Angeles, CA'});
//zipCodes.length = 2
You could also (depending on a large variety of current unknowns) populate a second array that holds indexes (indices?) of populated entries in the first array.
So you could have a method that zooms over the entire array once, populating the second array:
var indexesNext = 0;
var indexes = new Array();
for(i = 0 ; i < zipCodes.length ; i++)
if(zipCodes[i] != null) indexes[indexesNext++] = i;
Or you could use add and delete accessor methods on the zipCodes array to update the indexes array if you're expecting it to change often.
Or you could use the Javascript sort() method on the array, and iterate through it until you hit an empty cell. You could use this to optimise the previous techniques as well (YMMV on this built-in method's efficiency, but the new Javascript engines in FFX 3.1 and Chrome will certainly do quite well, I'm sure.)
Alternatively, you can iterate a sparse array with forEach
. To count the number of used slots, you can use Object.keys()
, provided that you don't define other (read: non-integer) own enumerable properties on the array, as they would be counted too:
const zipCodes = [];
zipCodes[10006] = 'New York, NY';
zipCodes[90210] = 'Los Angeles, CA';
console.log("There are", Object.keys(zipCodes).length, "zip codes:");
zipCodes.forEach((city, zipCode) => console.log(zipCode, city));
With the same caveat (of defining other own enumerable properties), you can use Object.entries
in a for..of
loop:
const zipCodes = [];
zipCodes[10006] = 'New York, NY';
zipCodes[90210] = 'Los Angeles, CA';
console.log("There are", Object.keys(zipCodes).length, "zip codes:");
for (const [zipCode, city] of Object.entries(zipCodes)) {
console.log(zipCode, city);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889567a4599321.html
评论列表(0条)