“splatać” Kod odpowiedzi

Dodaj element do tablicy za pomocą 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)
Hurt Hummingbird

splatać

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

splatać

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

Tablica#splice

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

Tablica maszynopisu splice

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

Odpowiedzi podobne do “splatać”

Pytania podobne do “splatać”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu