By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,172 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,172 IT Pros & Developers. It's quick & easy.

Remove array items iteratively

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Derek Basch <db****@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

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.

Jul 23 '05 #2

P: n/a
"Derek Basch" <db****@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

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.
Jul 23 '05 #3

P: n/a
Derek Basch wrote:
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


<!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>

Jul 23 '05 #4

P: n/a
> 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?


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
Jul 23 '05 #5

P: n/a

Douglas Crockford wrote:
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[i]){
var flag = true
};
};

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

Jul 23 '05 #6

P: n/a
>>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[i]){
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
Jul 23 '05 #7

P: n/a
Douglas Crockford wrote:
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[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

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.