Category Archives: Array

JavaScript / Any language: Delete a specific array element and shift other elements after deletion

How to delete a specific array element and shift other elements after deletion in JavaScript or any other programming languages

Here I have used an array , to be precise, predefined fixed array I have used here.
Initial array is [15,10,20,22,1,3,6].

Then I have decided to delete an item based on a comparison, here I have chosen to delete item 20.
After deletion the array will be : [15,10,22,1,3,6]

Initially after assigning the array by the statement var myArray = [15,10,20,22,1,3,6];
myArray[0]=15,
myArray[1]=10,
myArray[2]=20,
myArray[3]=22,
myArray[4]=1,
myArray[5]=3,
myArray[6]=6 and
array length = 7 found by the statement myArray.length;

After deletion the the item 20 will be deleted i.e myArray[2] and (after delete) myArray[2] will hold (before delete) myArray[3] i.e 22, (after delete) myArray[3] will hold (before delete) myArray[4] value i.e 1.

So , after deletion the array elements will be as follows :
myArray[0]=15
myArray[1]=10
myArray[2]=22
myArray[3]=1
myArray[4]=3
myArray[5]=6
array length = 6

Below we have used JavaScript splice() method, but this shifting and delete can be done without this method. I am giving you a hint.

I have not used this splice() method in the following JavaScript code snippet, you can use it also instead of the code in Gray.

Make deletion and shifting without splice() method in JavaScript. The following algorithm will work in any programming language.

The algorithm :
Step1: first reach the item which you want to delete by a FOR LOOP,
Step 2: Then get the ARRAY INDEX of that element.
Step 3: Then assign next array element to that element. Assign next array element to that element to shift and continue the process.

// Make shifting a global variable.
var shifting = 0;
for(var i = 0; i <= myArray.length-1; i++) { if(myArray[i] == 20) shifting = 1; if(shifting == 1) myArray[i] = myArray[i+1]; }

Delete an array element and shift the rest of the element by the help of SPLICE() method in JavaScript.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
var myArray = [15,10,20,22,1,3,6];
document.write("<br />"+myArray);
document.write("<br />Before delete Array Length : "+myArray.length);
for(var i = myArray.length-1; i >= 0; i–)
{
if(myArray[i] == 20)
{
myArray.splice(i,1);
}
}
document.write("<br />After deleting item 20 the array becomes : "+myArray);
document.write("<br />Array Length : "+myArray.length);
</script>
</head>

<body>
</body>
</html>

Sorting arrays in Javascript, Numeric sorting

If you normally sort an array, then it is sorted alphabetically or lexically.

<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
var myarray = [10,1,100,23,2,33,4];
myarray.sort();
document.write(myarray);
</script>
</head>

<body>
</body>
</html>

The output will be : 1,10,100,2,23,33,4
Because the array myarray is sorted alphabetically.

But if you want to sort it numerically then pass a function in argument as mentioned below.

<html>
<head>
<script type="text/javascript">
var myarray = [10,1,100,23,2,33,4];
myarray.sort(function f(a,b) { return a-b; });
document.write(myarray);
</script>
</head>

<body>
</body>
</html>