/    Sign up×
Community /Pin to ProfileBookmark

Remove element from array

Two questions actually:

1: How can I start an array on [1] instead of [0].
element.lenght returns three in this:

var element = new Array()
element[1] = “michelle”;
element[2] = “de beer”;

2: How can I remove an element totally from the array?
What I do now is to set the array-element to null, but it only replaces the contents of the array with null.

Try the code below to see my point:

var element = new Array()
element[1] = “michelle”;
element[2] = “de beer”;
element[2] = null;
alert(element.length);
for (var i = 1; i < element.length ; i++) {
alert(element[i]);
}

I want the alert(element.length); to alert 2, not 3…

Any thoughts?
// Michelle

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Mar 27.2003 — First of all I dont think thee is a straight forward way of doing this if you want to remove an element in the middle of the array.For removing the last element of the array you can use 2 methods

myArray.length = newLength;

will remove all the elements beyond the newlength value.

another way of getting this done is using Array,pop() javascript function,this function removes and returns the last element in array

myArray.pop() will remove the last element.

Hope this helps

Cheers

Khalid
Copy linkTweet thisAlerts:
@pyroMar 27.2003 — Change you alert line to this:

[color=darkblue]alert(element.length-1);[/color]
×

Success!

Help @michelle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...