splice() : it is used to delete an item from an array that effects array length.
delete() : it replaces the item with undefined instead of the removing it from the array. It means that it does not effect the array length.
note:Instead of using delete we should use the splice method to delete the element from an array.
Example:
var objects = [10, 54 ,'b' , 3 , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ];
objects.length; // return 10
delete items[3]; // return true
objects.length; // return 10
/* items will be equal to [10, 54 ,'b' , undefined 1 , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ] */
Use
var objects = [10, 54 ,'b' , 3 , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ];
objects.length; // return 10
objects.splice(3,1) ;
objects.length; // return 9
/* items will be equal to [10, 54 ,'b' , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ]; */
The delete method should be used to delete an object property.
0 Comment(s)