Javascript odstrániť položky index vráti pole

Príklady kódu

11
0

N

// remove element at certain index without changing original
let arr = [0,1,2,3,4,5]
let newArr = [...arr]
newArr.splice(1,1)//remove 1 element from index 1
console.log(arr) // [0,1,2,3,4,5]
console.log(newArr)// [0,2,3,4,5]
3
0

N

//you can use two functions depending of what you want to do:

let animals1 = ["dog", "cat", "mouse"]
delete animals1[1]
/*this deletes all the information inside "cat" but the element still exists
so now you'll have this:*/
console.log(animals1)//animals1 = ["dog", undefined, "mouse"]

//if you want to delete it completely, you have to use array.splice:

let animals2 = ["dog", "cat", "mouse"]
animals2.splice(1, 1)
/*the first number means the position from which you want to start to delete
and the second is how much elements will be deleted*/
console.log(animals2)//animals2 = ["dog", "mouse"]
/*Now you don't have undefined
If you did this:*/
let animals3 = ["dog", "cat", "mouse"]
animals3.splice(0, 2)//you'll have this:
console.log(animals3)//animals 3 = "mouse"
/*This happens because I put a 2 in the second parameter so it deleted
two elements from position 0
Try copying this code in your console and whatch*/

V iných jazykoch

Táto stránka je v iných jazykoch

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................