473,770 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing array item

I have the following. There must be something less cumbersome without push
pop. The function parameter obj is the event.srcElemen t and has the
attributes(prop erties?) picNum and alb pinned to it. The function is part of
a process to delete the selected obj, a photo in this case from the album
vis albumArr. TIA
Jimbo
function Album(albumName ) {
this.name=album Name;
this.paths=new Array();
}
var albumArr(); // holds an array of Albums();
function removeArrSrc(ob j) {
var dummy=new Array();
for(i=0;i<album Arr.length;i++) {
dummy[i]=new Album(albumArr[i].name);
var el=albumArr[i].paths;
for(var j=0;j<el.length ;j++) {
if(dummy[i].name==obj.alb) {
if(j==obj.picNu m) continue;
}
var len=dummy[i].paths.length;
dummy[i].paths[len]=el[j];
}
}
albumArr=dummy;
}

Jul 23 '05 #1
13 2350
On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale <ph****@netvisi on.net.il>
wrote:

012345678901234 567890123456789 012345678901234 567890123456789 0123456789
I have the following. There must be something less cumbersome without
push pop. The function parameter obj is the event.srcElemen t
Is this for the Web, or just an IE-only environment?
and has the attributes(prop erties?) picNum and alb pinned to it. The
function is part of a process to delete the selected obj, a photo in
this case from the album vis albumArr. TIA function Album(albumName ) {
this.name=album Name;
this.paths=new Array();
}
var albumArr(); // holds an array of Albums();
I don't know what that's supposed to be, but I'm certain it'll cause an
error on most browsers. Perhaps you meant:

var albumArr = [];
function removeArrSrc(ob j) {
var dummy=new Array();
for(i=0;i<album Arr.length;i++) {
dummy[i]=new Album(albumArr[i].name);
var el=albumArr[i].paths;
for(var j=0;j<el.length ;j++) {
if(dummy[i].name==obj.alb) {
if(j==obj.picNu m) continue;
}
var len=dummy[i].paths.length;
dummy[i].paths[len]=el[j];
}
}
albumArr=dummy;
}


How about:

function removeArrSrc(o) {
var i = 0, n = albumArr.length ;

// Find the element, 'o'
while(i < n && albumArr[i] != o) {++i;}
// If 'o' exists in the array, 'i' will now point it
if(i < n) {
// Don't want to reach the last element
--n;
// Now shift the remaining elements down
while(i < n) {albumArr[i] = albumArr[i + 1]; ++i;}
// Finally, truncate the array
albumArr.length = n;
}
}

which is removes the element in-place. You could also use:

function removeArrSrc(o) {
var i = 0, n = albumArr.length ;

// Find the element, 'o'
while(i < n && albumArr[i] != o) {++i;}
// If 'o' exists in the array, 'i' will now point it
if(i < n) {
albumArr = albumArr.slice( 0, i).concat(album Arr.slice(i + 1));
}
}

which might be a little quicker (due to being performed by the browser),
but probably isn't (due to the resulting array allocations).

I haven't tested these, by the way.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsdjinqrx x13kvk@atlantis ...
On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale <ph****@netvisi on.net.il>
wrote:

012345678901234 567890123456789 012345678901234 567890123456789 0123456789
I have the following. There must be something less cumbersome without
push pop. The function parameter obj is the event.srcElemen t
Is this for the Web, or just an IE-only environment?


Ideally it would be generic although currently I'm writing an HTA.
and has the attributes(prop erties?) picNum and alb pinned to it. The
function is part of a process to delete the selected obj, a photo in
this case from the album vis albumArr. TIA

function Album(albumName ) {
this.name=album Name;
this.paths=new Array();
paths array holds image paths
}
var albumArr(); // holds an array of Albums();


I don't know what that's supposed to be, but I'm certain it'll cause an
error on most browsers. Perhaps you meant:

var albumArr = [];

Sorry! It is declared var albumArr=new Array();
function removeArrSrc(ob j) {
var dummy=new Array();
for(i=0;i<album Arr.length;i++) {
dummy[i]=new Album(albumArr[i].name);
var el=albumArr[i].paths;
for(var j=0;j<el.length ;j++) {
if(dummy[i].name==obj.alb) {
if(j==obj.picNu m) continue;
}
var len=dummy[i].paths.length;
dummy[i].paths[len]=el[j];
}
}
albumArr=dummy;
}


How about:

function removeArrSrc(o) {
var i = 0, n = albumArr.length ;

// Find the element, 'o'
while(i < n && albumArr[i] != o) {++i;}

the array albumArr contains an array of Album objects. I need to delete
the path for one image.
the reference obj (here 'o') has the album name and the index in the
paths array pinned as attributes
I'm searching therefore for o.alb (the album name) and o.picNum the
offset into the paths array of the Album object.
while(i<n && albumArr[i].name != o.alb) {++i} should work to
find the album
// If 'o' exists in the array, 'i' will now point it
if(i < n) {
// Don't want to reach the last element
--n;
// Now shift the remaining elements down
while(i < n) {albumArr[i] = albumArr[i + 1]; ++i;}
// Finally, truncate the array
albumArr.length = n;
}
}
This would delete the entire album. I need to delete the image from the
paths array of the album. The slice method with a regex is what I'm looking
for but I'm just getting into regex.
which is removes the element in-place. You could also use:

function removeArrSrc(o) {
var i = 0, n = albumArr.length ;

// Find the element, 'o'
while(i < n && albumArr[i] != o) {++i;}
// If 'o' exists in the array, 'i' will now point it
if(i < n) {
albumArr = albumArr.slice( 0, i).concat(album Arr.slice(i + 1));
}
}

which might be a little quicker (due to being performed by the browser),
but probably isn't (due to the resulting array allocations).
I hope I've explained myself better this time. Thanks for the above particularly because I haven't written the funtion to delete an album yet.
I'm sure this will do the job.
Jimbo I haven't tested these, by the way.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #3
On Tue, 31 Aug 2004 09:02:32 +0200, J. J. Cale <ph****@netvisi on.net.il>
wrote:
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsdjinqrx x13kvk@atlantis ...
On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale
<ph****@netvisi on.net.il> wrote:
[snip]
event.srcElemen t
Is this for the Web, or just an IE-only environment?


Ideally it would be generic although currently I'm writing an HTA.


The reason I asked is that event.srcElemen t is IE-only. You'll also have
to check event.target, and possibly other properties, for the code to work
with other browsers. However, since you're writing a HTA, it isn't of much
concern at the moment.

[snip]
the array albumArr contains an array of Album objects. I need to
delete the path for one image.
Sorry.
the reference obj (here 'o') has the album name and the index in the
paths array pinned as attributes
I'm searching therefore for o.alb (the album name) and o.picNum the
offset into the paths array of the Album object.
while(i<n && albumArr[i].name != o.alb) {++i} should work
to find the album
Yes, it would.

[snip]
This would delete the entire album. I need to delete the image from the
paths array of the album. The slice method with a regex is what I'm
looking for but I'm just getting into regex.


I don't think that a regular expression would be terribly useful. Of
course, I'm not seeing the whole picture, so I could be wrong.

[snip]

function Album(albumName ) {
this.name = albumName;
this.paths = [];
}
Album.prototype .removePath = function(i) {
for(var n = this.paths - 1; i < n; ++i) {
this.paths[i] = this.paths[i + 1];
}
this.paths.leng th = n;
};

function removeArrSrc(o) {
for(var i = 0, n = albumArr.length ; i < n; ++i) {
if(albumArr[i].name == o.alb) {
albumArr[i].removePath(o.p icNum);
break;
}
}
}

I forsee a possible problem here. Say that you delete the first path
(picNum 0). In the paths array, all of the elements will shift making the
second path first, third path second, etc. If you then delete the second
path (picNum 1), you'll actually be deleting the third path from the
original list. Do you account for that?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
J. J. Cale wrote:
I have the following. There must be something less cumbersome without push
pop. The function parameter obj is the event.srcElemen t
which is IE only, use

function foo(e)
{
if (!e)
e = event;

if (e)
{
var t = e.target ? e.target : e.srcElement;
if (t)
{
// do something with t
}
}
}

<... on...="foo(even t);" ...>...</...>

instead.
and has the attributes(prop erties?) picNum and alb pinned to it. The
function is part of a process to delete the selected obj, a photo in
this case from the album vis albumArr. TIA
Jimbo
function Album(albumName ) {
this.name=album Name;
this.paths=new Array();
}
var albumArr(); // holds an array of Albums();
Invalid syntax. () is the call operator and can only be applied to
functions/methods.

var albumArr;

Watch the JavaScript Console or the status bar of the browser window!
function removeArrSrc(ob j) {
var dummy=new Array();
for(i=0;i<album Arr.length;i++) {
Note that `i' is declared global here, causing mostly undesired
side effects. Use the `var' keyword (and optimize a bit):

for (var i = 0, len = albumArr.length ; i < len; i++)
{
// ...
}

Since order does not seem to matter here, this can be
further optimized:

for (var i = albumArr.length ; i--; 0)
{
// ...
}

This loops from the last element to the first.
dummy[i]=new Album(albumArr[i].name);
[...]


To remove an array element, use the "delete" operator:

delete albumAr[42];

The array size will not be reduced, but further references
to albumAr[42] will return `undefined' instead of an Album
object reference which you can detect in your loop:

for (var i = albumAr.length; i--; 0)
{
if (albumAr[i])
{
delete albumAr[i];
}
}

More sophisticated but requires JavaScript 1.1 and compatibles:

for (var i = albumAr.length; i--; 0)
{
if (typeof albumAr[i] != "undefined" )
{
delete albumAr[i];
}
}

or

for (var i = albumAr.length; i--; 0)
{
if (albumAr[i] && albumAr[i].constructor == Album)
{
delete albumAr[i];
}
}

Even more sophisticated but requires an ECMAScript 3 or later
implementation (e.g. JavaScript 1.5, JScript 5.6):

for (var i = albumAr.length; i--; 0)
{
if (albumAr[i] instanceof Album)
{
delete albumAr[i];
}
}

You may want to add further conditions which must be matched
before the element will be deleted: your search parameters.
HTH

PointedEars

P.S.
Falsifying header information is not appropriate behavior (violating
several Usenet/Internet standards, the Netiquette and most certainly
the Acceptable Use Policy of service providers). It only *supports*
spammers' doing: <http://www.interhack.n et/pubs/munging-harmful/>
--
If they outlaw encryption, only outlaws will
62Xq3mrg45x69A9 6yvxi70gXrSdD+S Cw
Jul 23 '05 #5
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:

A few nitpicks:
function foo(e)
{
if (!e)
e = event;
these two lines are not necessary if you do:
<... on...="foo(even t);" ...>...</...>
The event will be the argument to the function in all browsers (since
you pass it explicitly, and they all agree that "event" refers to the
event in an intrinsic event handler). It is needed (in IE) only when
you assign the function directly, as in:
something.onwha tever = foo;
Even more sophisticated but requires an ECMAScript 3 or later
implementation (e.g. JavaScript 1.5, JScript 5.6):

for (var i = albumAr.length; i--; 0)


This would start "i" out at albumAr.length, which is one past
the last element of the array. It should probably be
...(var i = albumAr.length - 1; ...

The "0" can also be omitted (all three expressions in a "for"
construct are optional).
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
function foo(e)
{
if (!e)
e = event;
these two lines are not necessary if you do:
<... on...="foo(even t);" ...>...</...>


[...] It is needed (in IE) only when
you assign the function directly, as in:
something.onwha tever = foo;


Correct, I confused both.
Even more sophisticated but requires an ECMAScript 3 or later
implementation (e.g. JavaScript 1.5, JScript 5.6):

for (var i = albumAr.length; i--; 0)


This would start "i" out at albumAr.length, which is one past
the last element of the array. It should probably be
...(var i = albumAr.length - 1; ...


No, it should not. The value of i is decreased the first time
the loop is executed.
The "0" can also be omitted (all three expressions in a "for"
construct are optional).


It cannot be omitted, the expressions are not optional in all
implementations .
PointedEars
--
Talk to the hand, the hand doesn't listen...
Jul 23 '05 #7
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn writes:
for (var i = albumAr.length; i--; 0)


This would start "i" out at albumAr.length, which is one past
the last element of the array. It should probably be
...(var i = albumAr.length - 1; ...

<snip>

No, the first two expressions are fine. The - i-- - post decrements the
counter before its first use (assuming length was not zero, else there
would be no array access) leaving it the equivalent of length-1 when it
is first used.

Rihcard.

Jul 23 '05 #8
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
No, it should not. The value of i is decreased the first time
the loop is executed.
Ach, yes. The decrement is in the test, which is (apparently)
confuzing enough that I won't recommend it (for anything I have
to read :)
It cannot be omitted, the expressions are not optional in all
implementations .


Can you name one where it isn't?

All three expressions are optional in Javascript 1.0 (tested in
Netscape 2.02) and in ECMAScript v1.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #9
Thomas 'PointedEars' Lahn wrote:
Lasse Reichstein Nielsen wrote:
<--snip-->
The "0" can also be omitted (all three expressions in a "for"
construct are optional).

It cannot be omitted, the expressions are not optional in all
implementations .


Can you quote one that does not allow them to be optional?
--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2474
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
66
4146
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It started by an example from me, that was far from complete and with typing errors, but basically it had the meaning to show that removing controls reversible was the nicest method. This was a lets say conclusion (but what is that in a newsgroup) in a...
7
1753
by: Adam Honek | last post by:
Is there a direct way to remove one entry from a one dimensional array? I keep looking but either my eyes are funny or it isn't there. Must we really create a temp array and copy all but 1 of the entries over to delete one entry? All I want to do is remove an entry at a specified index and then have the ubound(array) go down -1.
6
6114
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
9
8251
by: Rob Meade | last post by:
Hi all, I have an array of lets say 10 items, I now want to remove an item, lets say from somewhere in the middle, based on one of the element values equalling a value I specify - is there a "clever" and "quick" way of doing this or, will I need to iterate through it looking for a match, and build another array where I dont find the match etc... Any advice appreciated..
9
2590
by: Rob Meade | last post by:
Hi all, Ok - so I've got the array thing going on, and the session thing going on, and up until now its all been ok. I've got my view basket page which is displaying 3 rows (as an example) - I have an input box on each row enabling the user to update the quantity. If the enter a zero I need to remove the item from the array....
5
10085
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException because the ListView seems to be trying to "re-select" items that are no longer there! for example, giventhat I have 3 items in my list: Select the first and remove it - no problem.
0
1979
by: Mike | last post by:
Hi, I have a collection object bound to a data grid, after I remove an item from the collection, the minute I click on the datagrid I get an error saying the specified argument was out of the range of valid values. After I remove the element from the collection I clear the databindings from the data grid and rebind it but as far as I can tell the collection seems to be the right size but for some reason when I
7
2845
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.