PDA

View Full Version : Additional method remove() in standard array class



tosha.
8 Sep 2010, 3:04 AM
ExtJs creates a new method remove() in standard class an array. The count of elements in an array after that is defined correctly. However cycle for in together with array items returns also a remove() function. As though you have advised to solve this problem correctly?



var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

for (var x in mycars)
{
alert(mycars[x]);
}


result:


"Saab"
"Volvo"
"BMW"
and remove() code...

Condor
8 Sep 2010, 3:19 AM
Basic javascript: for(x in y) enumerates over the properties of y and not over the items.

For an array you should use:

for (var x = 0; x < mycars.length; x++) {
alert(mycars[x]);
}

tosha.
8 Sep 2010, 3:25 AM
Thank you. But what can I do if I need and keys? For example:

mycars['firstcar'] = "Saab";
mycars['bestcar'] = "Volvo";
mycars['othercar'] = "BMW";



for (var x in mycars)
{
alert(x +' - '+mycars[x]);
}

Condor
8 Sep 2010, 3:31 AM
That's not an array. That is an object!


var mycars = {}; // or new Object()
mycars['firstcar'] = "Saab";
mycars['bestcar'] = "Volvo";
mycars['othercar'] = "BMW";
for (var x in mycars) {
if (mycars.hasOwnProperty(x)) {
alert(x +' - '+mycars[x]);
}
}

tosha.
8 Sep 2010, 4:08 AM
Thank you very much.