Home javascript Deleting JavaScript array elements

Deleting JavaScript array elements

Author

Date

Category

It is required to remove the elements of the array from the position1 to position2

For example 0 1 2 3 4 5 6 (position1 = 1; position2 = 3) We obtain an array (0 1 3 4 5 6)

Split Ohm does not go


Answer 1, Authority 100%

For these purposes, the SPLICE method is used with two arguments: the initial index of the removed elements and the number of removed items. For example

a.splice (2, 1)

Removes one element with index 2.

The method returns an array object with remote elements.

Here is a demonstration example

& lt;! doctype HTML / & GT;
& lt; html & gt;
& lt; Head & gt;
  & lt; Title & gt; Array SPLICE & LT; / Title & GT;
& lt; / Head & gt;
& lt; Body & gt;
  & lt; script & gt;
    var a = [0, 1, 2, 3, 4, 5, 6];
    Alert ("Original A:" + a.tostring ());
    // OR Alert ("Original A:" + A);
    var b = a.splice (2, 1);
    Alert ("After Deleting A:" + a.tostring ());
    Alert ("Array Of Deleted Elements B:" + B.Tostring ());
  & lt; / script & gt;
& lt; / body & gt;
& lt; / html & gt;

That is if you need to remove elements in the array between the elements with indexes index1 and index2, then you write

a.splice (index1 + 1, index2 - index1 - 1);

where index2 & gt; index1.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions