2023-05-08

Javascript :: Add an item at any position in an array

It's easy, just use splice():
var myArray = [1, 2];
var valueToInsert = 1.5;
var indexToInsert = 1;
var deleteCount = 0;
myArray.splice(indexToInsert, deleteCount, valueToInsert);
console.log(JSON.stringify(myArray));
//outputs: [1, 1.5, 2]

No comments:

Post a Comment

Please provide any feedback or post questions here...

Javascript :: comparison operators, using == vs === and != vs !==

Comparison operators are distinguished following simple logic, whether data type is being included or not. Essentially if it is === or !==, ...