“épissure” Réponses codées

Ajouter un élément au tableau à l'aide d'épissure

//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)
Hurt Hummingbird

épissure

//Example 1:

//Remove 1 element at index 3
let myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon']
let removed = myFish.splice(3, 1)

//Result
// myFish is ["angel", "clown", "drum", "sturgeon"]
// removed is ["mandarin"]

//Example 2:

//Remove 1 element at index 2, and insert "trumpet"
let myFish = ['angel', 'clown', 'drum', 'sturgeon']
let removed = myFish.splice(2, 1, 'trumpet')

//Result
// myFish is ["angel", "clown", "trumpet", "sturgeon"]
// removed is ["drum"]
Inquisitive Iguana

épissure

const sing = "I am a disco dancer"
console.log(sing.slice(0,5))// I am
Shamimzz

Array # Splice

let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Hilarious Heron

Tableau d'épissage dactylographié

var arr = ["orange", "mango", "banana", "sugar", "tea"];  
var removed = arr.splice(2, 0, "water");  
console.log("After adding 1: " + arr );  
console.log("removed is: " + removed); 
          
removed = arr.splice(3, 1);  
console.log("After removing 1: " + arr );  
console.log("removed is: " + removed);
Exuberant Earthworm

Réponses similaires à “épissure”

Questions similaires à “épissure”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code