Javascript odstránenie položky z poľa

Príklady kódu

214
0

N

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]
16
0

N

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var removed = arr.splice(2,2);
/*
removed === [3, 4]
arr === [1, 2, 5, 6, 7, 8, 9, 0]
*/
14
0

N

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1);
}

// array = [2, 9]
console.log(array); 
9
0

N

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var filtered = array.filter(function(value, index, arr){
    return value > 5;
});
//filtered => [6, 7, 8, 9]
//array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
8
0

N

let forDeletion = [2, 3, 5]

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => !forDeletion.includes(item))
// !!! Read below about array.includes(...) support !!!

console.log(arr)
// [ 1, 4 ]
2
0

N

// - - - - - - - - - - -
// Remove Last Element (pop)
// - - - - - - - - - - -
// example (remove the last element in the array)
let yourArray = ["aaa", "bbb", "ccc", "ddd"];
yourArray.pop(); // yourArray = ["aaa", "bbb", "ccc"]

// syntax:
// <array-name>.pop();

// - - - - - - - - - - -
// Remove First Element (shift)
// - - - - - - - - - - -
// example (remove the last element in the array)
let yourArray = ["aaa", "bbb", "ccc", "ddd"];
yourArray.shift(); // yourArray = ["bbb", "ccc", "ddd"]

// syntax:
// <array-name>.shift();

Podobné stránky

Podobné stránky s príkladmi

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