Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add element to array using splice

//we can add elements to array using splice

const strings=['a','b','c','d']

//go the 2nd index,remove 0 elements and then add 'alien' to it
strings.splice(2,0,'alien')

console.log(strings) //["a", "b", "alien", "c", "d"]

//what is the time complexity ???
//worst case is O(n). if we are adding to end of array then O(1)
//in our example we added to the middle of array so O(n/2)=>O(n)
Comment

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;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js order alphabetically 
Javascript :: express get url parameters 
Javascript :: js replace all spaces 
Javascript :: vue js store and retrieve api data to localstorage 
Javascript :: js sort string array 
Javascript :: how to pass token in laravel in jquery ajax 
Javascript :: javascript delete key from object 
Javascript :: refresh a page in jquery 
Javascript :: adonis count with where 
Javascript :: array sort by alphabetical javascript 
Javascript :: map images from folder react 
Javascript :: redirect via javascript 
Javascript :: redirect with javascript to another page 
Javascript :: backgroundcolor react 
Javascript :: js sort by property 
Javascript :: cypress support ability to set viewport in `before` 
Javascript :: check if input is valid 
Javascript :: set delay javascript 
Javascript :: node js express mongodb find all documents 
Javascript :: jquery merge objects 
Javascript :: javascript array remove empty strings 
Javascript :: javascript check if set 
Javascript :: js select disabled 
Javascript :: random in a range js 
Javascript :: vuex v-model 
Javascript :: nock CORS error 
Javascript :: get request react 
Javascript :: asp.net core 3.1 convert system.collections.generic.list`1[system.string] to javascript 
Javascript :: convert iso 8601 to utc javascript 
Javascript :: asignar val select2 js 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =