Search This Blog

2023/07/25

How to emply array




 
 
var arrayList =  ['a', 'b', 'c', 'd', 'e', 'f'];


console.log("Before:",arrayList)

arrayList.splice(0,arrayList.length)

console.log("After:",arrayList)

output:
Before: [ 'a', 'b', 'c', 'd', 'e', 'f' ]
After: []


Multiple Element placement inbetween

let arr = [1, 4, 9, 25, 36];
arr.splice(3, 0, 16,17,18);
console.log(arr);

Output:

   [
   1,  4,  9, 16,
  17, 18, 25, 36
]

splice(3, 0, 16) //means:

3index where to insert
0don't remove anything if 1 remove one element,if 2 remove 2 elemenr
 16,17,18value to insert


No comments:

Post a Comment