JavaScript Program To Insert Item In An Array - Programiz
Maybe your like
JavaScript Program to Insert Item in an Array
To understand this example, you should have the knowledge of the following JavaScript programming topics:
- JavaScript Array splice()
- JavaScript for loop
- JavaScript Array
Example 1: Add Item to Array Using splice()
// program to insert an item at a specific index into an array function insertElement() { let array = [1, 2, 3, 4, 5]; // index to add to let index = 3; // element that you want to add let element = 8; array.splice(index, 0, element); console.log(array); } insertElement();Output
[1, 2, 3, 8, 4, 5]In the above program, the splice() method is used to insert an item with a specific index into an array.
The splice() method adds and/or removes an item.
In the splice() method,
- The first argument specifies the index where you want to insert an item.
- The second argument (here 0) specifies the number of items to remove.
- The third argument specifies the element that you want to add to an array.
Example 2: Add Item to Array Using for Loop
// program to insert an item at a specific index into an array function insertElement() { let array = [1, 2, 3, 4]; // index to add to let index = 3; // element that you want to add let element = 8; for (let i = array.length; i > index; i--) { //shift the elements that are greater than index array[i] = array[i-1]; } // insert element at given index array[index] = element; console.log(array); } insertElement();Output
[1, 2, 3, 8, 4]In the above program,
- The for loop is used to iterate through the array elements.
- The element is added to the given index.
- All the elements whose index is greater than the given index are shifted one step to the right.
Also Read:
- JavaScript Program to Add Element to Start of an Array
- JavaScript Program to Append an Object to An Array
Tag » Add Element Array Js
-
How To Append Something To An Array? - Stack Overflow
-
JavaScript Array Push() Method - W3Schools
-
How To Add To An Array With The Push, Unshift, And Concat Functions
-
totype.push() - JavaScript - MDN Web Docs
-
5 Way To Append Item To Array In JavaScript
-
How To Add Elements Into An Array In JavaScript - Linux Hint
-
How To Add New Elements At The Beginning Of An Array In JavaScript
-
JavaScript: How To Insert Elements Into A Specific Index Of An Array
-
Javascript Array Push: How To Add Element In Array - AppDividend
-
JavaScript Program To Add Element To Start Of An Array - Programiz
-
Add An Element To An Array In JavaScript - Mastering JS
-
TypeScript - Array Push() - Tutorialspoint
-
Add Element To Array At Specific Index In JavaScript | Bobbyhadz
-
How To Insert An Element In A Specific Index In JavaScript Array
-
JavaScript Array Splice(): Delete, Insert, And Replace
-
How To Add Item To An Array At A Specific Index In JavaScript
-
Javascript Tutorial | Adding Elements To Array In JavaScript - Morioh
-
Add An Item To The Beginning Of An Array In JavaScript Or Node.js
-
How To Add Element To UseState Array In JavaScript - Simplernerd