Connecting Tech Pros Worldwide Forums | Help | Site Map

Remove array items iteratively

Derek Basch
Guest
 
Posts: n/a
#1: Jul 23 '05
I can remove objects from an array by doing this:

for (i in oCache){
if (i == "test"){
delete oCache[i];
};
};

However, the array's length property is unaffected.

If I use splice like so:

for (i in oCache){
if (i == "test"){
oCache.splice(i, 1);
};
};

It breaks because oCache's length is altered within the loop by splice.

How do I iterate though an array, remove items AND change the length
property?

Thanks,
Derek Basch


Stephen Chalmers
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Remove array items iteratively


Derek Basch <dbasch@yahoo.com> wrote in message
news:1109205136.758427.262560@g14g2000cwa.googlegr oups.com...[color=blue]
> I can remove objects from an array by doing this:
>
> for (i in oCache){
> if (i == "test"){
> delete oCache[i];
> };
> };
>
> However, the array's length property is unaffected.
>
> If I use splice like so:
>
> for (i in oCache){
> if (i == "test"){
> oCache.splice(i, 1);
> };
> };
>
> It breaks because oCache's length is altered within the loop by splice.
>
> How do I iterate though an array, remove items AND change the length
> property?
>
> Thanks,
> Derek Basch
>[/color]
If you test the length on each iteration, it won't matter if it changes:

for (var i=0; i<oCache.length; i++)
if (i == "test")
oCache.splice(i, 1);

--
S.C.



Jarmo
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Remove array items iteratively


"Derek Basch" <dbasch@yahoo.com> wrote in message
news:1109205136.758427.262560@g14g2000cwa.googlegr oups.com...[color=blue]
>
> It breaks because oCache's length is altered within the loop by splice.
>
> How do I iterate though an array, remove items AND change the length
> property?[/color]

The simplest way to do this is to iterate from bottom to top rather than top
to bottom. For example:

for (ii = blob.length - 1; ii >= 0; ii--)
{
if (blob[ii] meets some condition)
{
remove blob[ii] from array
}
}

This way the changing value of blob.length has no impact, nor does the
removal of items from the array.


RobB
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Remove array items iteratively


Derek Basch wrote:[color=blue]
> I can remove objects from an array by doing this:
>
> for (i in oCache){
> if (i == "test"){
> delete oCache[i];
> };
> };
>
> However, the array's length property is unaffected.
>
> If I use splice like so:
>
> for (i in oCache){
> if (i == "test"){
> oCache.splice(i, 1);
> };
> };
>
> It breaks because oCache's length is altered within the loop by[/color]
splice.[color=blue]
>
> How do I iterate though an array, remove items AND change the length
> property?
>
> Thanks,
> Derek Basch[/color]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">

Array.prototype.dele = function()
{
for (var i = 0, l = arguments.length, arr = []; i < l; ++i)
{
for (var j = 0; j < this.length; ++j)
{
if (this[j] == arguments[i]
&& typeof this[j] == typeof arguments[i])
{
arr.push(this.splice(j, 1));
}
}
}
return arr;
}


var oCache = [
'el1' , 'el2' , '3' , 'test1' , 'el4' , 'test2'
];

a = window.alert;
a('oCache:\n\n' + oCache.join('\n'));
a('original length: ' + oCache.length);
a('call: oCache.dele(\'test1\')');
a('element removed: ' + oCache.dele('test1'));
a('oCache:\n\n' + oCache.join('\n'));
a('oCache length: ' + oCache.length);
a('call: oCache.dele(\'test2\')');
a('element removed: ' + oCache.dele('test2'));
a('oCache:\n\n' + oCache.join('\n'));
a('oCache length: ' + oCache.length);
a('call: oCache.dele(3)');
a('element removed: ' + oCache.dele(3));
a('oCache:\n\n' + oCache.join('\n'));
a('oCache length: ' + oCache.length);
a('call: oCache.dele(\'el1\', \'el2\', \'3\', \'el4\')');
a('elements removed: ' + oCache.dele('el1', 'el2', '3', 'el4'));
a('oCache:\n\n' + oCache.join('\n'));
a('oCache length: ' + oCache.length);

</script>
</head>
<body>
</body>
</html>

Douglas Crockford
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Remove array items iteratively


> I can remove objects from an array by doing this:[color=blue]
>
> for (i in oCache){
> if (i == "test"){
> delete oCache[i];
> };
> };
>
> However, the array's length property is unaffected.
>
> If I use splice like so:
>
> for (i in oCache){
> if (i == "test"){
> oCache.splice(i, 1);
> };
> };
>
> It breaks because oCache's length is altered within the loop by splice.
>
> How do I iterate though an array, remove items AND change the length
> property?[/color]

You should not be using an array if the subscripts are not integers.
That is what objects are for.

The array.length property is supposed to be 1 larger than the largest
integer subscript. The array.splice method has no effect on array.test.

If you misuse language features, you can easily get confused.

Perhaps your example is wrong, and you really are dealing with integer
subscripts. (There is a difference between test and "test".) In that
case, loop through backwards.

for (i = oCache.length - 1; i >= 0; i -= 1) {

See http://www.crockford.com/javascript/survey.html
Derek Basch
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Remove array items iteratively



Douglas Crockford wrote:
[color=blue]
> You should not be using an array if the subscripts are not integers.
> That is what objects are for.[/color]

Ahhh right, I always forget that. My question now is how do I test for
the existence of child objects if I cant test for something like
length? Here is what I currently am using but it seems kludgy.


for (var i in filter_cache){
if (filter_cache[i]){
var flag = true
};
};

if (flag != true) {
var a = getCache(sSortType, nColumn);
}
else {
var a = filter_cache
};


dTb

Douglas Crockford
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Remove array items iteratively


>>You should not be using an array if the subscripts are not integers.[color=blue][color=green]
>>That is what objects are for.[/color]
>
>
> Ahhh right, I always forget that. My question now is how do I test for
> the existence of child objects if I cant test for something like
> length? Here is what I currently am using but it seems kludgy.
>
>
> for (var i in filter_cache){
> if (filter_cache[i]){
> var flag = true
> };
> };
>
> if (flag != true) {
> var a = getCache(sSortType, nColumn);
> }
> else {
> var a = filter_cache
> };[/color]

I can't make sense of this. What are you trying to do?

It is bad to define a var twice in the same function.
Also, (flag != true) is better written as (!flag).
Also, for and if should not be followed by semicolon.
See http://www.crockford.com/javascript/lint.html
Derek Basch
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Remove array items iteratively


Douglas Crockford wrote:
[color=blue]
> I can't make sense of this. What are you trying to do?[/color]

Not suprising since I totally hosed my example code. Sorry. It Should
be:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>

<script language="javascript" type="text/javascript">

function findChildObjects(){
var filter_cache = Object;
var child_object = Boolean;

filter_cache.filter_1 = {
type: "Keyword"
};
filter_cache.filter_2 = {
type: "Substring"
};

for (var i in filter_cache){
if (typeof(filter_cache[i]) === "object"){
child_object = true;
}
}
if (child_object === true) {
alert("Children exist");
}
else {
alert("Children don't exist");
}
}

findChildObjects();

</script>

</body>
</html>

Is there a better way to test for the existence of child objects?

dTb

Closed Thread