473,408 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Remove array items iteratively

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
7 6460
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
"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
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
> 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

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
>>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Patrick G. | last post by:
Greetings all: ASP VB, SQL Svr 2000 I am pulling data from 3 tables. table1 holds item details table2 holds publication types and the item id from table1 table3 holds category types and...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
0
by: choyk1 | last post by:
I intended to save properties of an object to a Hashtable. In this case, keys and values are not fixed types and I cannot use SortedList. I have no idea what order the .NET framework add items to...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
2
by: Mamatha | last post by:
Hi I have an application with listview.When i click on one button the data will be displayed like this in the listview: colA colB colC ----- ----- ------...
3
by: Bill Nguyen | last post by:
I use the following example (from another post) and it seemed to work fine. However, when I add the syntax to remove the selected item from the senderbox, I got error....
19
by: brasilino | last post by:
Hi Folks: I've been looking (aka googling) around with no success. I need a usability beyond 'pop()' method when removing an Array elements. For example: oName = new...
7
by: Jacob JKW | last post by:
I need to iterate over combinations of n array elements taken r at a time. Because the value of r may vary quite a bit between program invocations, I'd like to avoid simply hardcoding r loops. I...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.