Remove array items iteratively

D

Derek Basch

I can remove objects from an array by doing this:

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

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
 
S

Stephen Chalmers

Derek Basch said:
I can remove objects from an array by doing this:

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

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

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);
 
J

Jarmo

Derek Basch said:
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?

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.
 
R

RobB

Derek said:
I can remove objects from an array by doing this:

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

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


<!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
&& typeof this[j] == typeof arguments)
{
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>
 
D

Douglas Crockford

I can remove objects from an array by doing this:
for (i in oCache){
if (i == "test"){
delete oCache;
};
};

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?


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
 
D

Derek Basch

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

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){
var flag = true
};
};

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


dTb
 
D

Douglas Crockford

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


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){
var flag = true
};
};

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


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
 
D

Derek Basch

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

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) === "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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top