0

My code :

<html>
<body>
<p id='demo'></p>
<script>
var array=['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
delete array[2];
document.getElementById('demo').innerHTML = array;
</script>
</body>
</html>

When I running this code...

The output Comma quotation showing ...after removing the 3rd value Comma quotation were not deleted after execution.

Help me to solve this (or) Suggest any other ways for deleting values in the specified index in js array.

georgeawg
  • 48,608
  • 13
  • 72
  • 95

4 Answers4

2

var a = ['BENZ','SKODA','BMW','MERCEDAS','VOLVO']; 
a.splice(2,1); 
console.log(a);
Andrej
  • 1,679
  • 1
  • 26
  • 40
1

Change delete array[2] to array.splice(2,1)

Regards.

cwlo
  • 39
  • 1
  • 5
1

I would say use:

array.splice(index, 1);

the "index" parameter is an index of the element you want to remove and "1" is a number of elements you want to remove. splice() will take care of the indexes of removed/added elements for you.

If you don't know the index of the element you can try:

array.indexOf("BMW");

Thanks.

Mehul Gayate
  • 344
  • 3
  • 7
-1
var array=['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
 array.pop[2];

By following you can delete the element in array .pop[]