“spleißen” Code-Antworten

Fügen Sie das Element mit Spleiß zu Array hinzu

//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

spleißen

//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

spleißen

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

Spleiß -TypeScript -Array

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

Ähnliche Antworten wie “spleißen”

Fragen ähnlich wie “spleißen”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen