How to use Array splice() Method In Javascript
The splice method is used to both remove and add elements from a specific index. An object can only be added without deleting any other element by specifying the second parameter to 0.
Syntax:
arr.splice(index, 0, objectName)
Example: In this example, we will add new objects to the array using the splice() method in Javascript.
function spliceFunction() {
list.splice(2, 0, "Angular", "SQL");
console.log(list);
}
let list = ["HTML", "CSS", "JavaScript"];
spliceFunction();
Output
[ 'HTML', 'CSS', 'Angular', 'SQL', 'JavaScript' ]
Add an Object to an Array in JavaScript
Adding an object to an array in JavaScript entails incorporating the object as an element within the array. This process is fundamental for dynamically managing data structures, enabling flexible storage and manipulation of complex data collections within the application.
Table of Content
- Using JavaScript Array push() Method
- Using JavaScript Array splice() Method
- Using JavaScript Array unshift() Method
- Using array concatenation (concat)
- Using JavaScript Array spread operator