how to insert an array into another array in javascript code example
Example 1: assign array to another array javascript
var ar = ["apple","banana","canaple"];
var bar = Array.from(ar);
alert(bar[1]);
Example 2: array insertion javascript
Array.prototype.insert = function ( index, item ) {
this.splice( index, 0, item );
};
Example 3: add elements to an array with splice
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.splice(2, 0, "Lemon", "Kiwi");
document.getElementById("demo").innerHTML = fruits;
}
Example 4: javascript push all elements of array to another array
var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);