Array odstrániť funkciu v JavaScripte

Príklady kódu

208
0

odstráňte konkrétny prvok z poľa

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"]
13
0

odstrániť prvok z poľa v js

var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

//removing element using splice method -- 
//arr.splice(index of the item to be removed, number of elements to be removed)
//Here lets remove Sunday -- index 0 and Monday -- index 1
  myArray.splice(0,2)

//using filter method
let itemToBeRemoved = ["Sunday", "Monday"]
var filteredArray = myArray.filter(item => !itemToBeRemoved.includes(item))
1
0

odstrániť pole js

var ar = [1, 2, 3, 4, 5, 6];ar.length = 4; // set length to remove elementsconsole.log( ar ); // [1, 2, 3, 4]
// //////////////////////////////////////
var ar = [1, 2, 3, 4, 5, 6];ar.pop(); // returns 6console.log( ar ); // [1, 2, 3, 4, 5]
// //////////////////////////////////////
var ar = ['zero', 'one', 'two', 'three'];ar.shift(); // returns "zero"console.log( ar ); // ["one", "two", "three"]
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];var removed = arr.splice(2,2);
// //////////////////////////////////////
["bar", "baz", "foo", "qux"]list.splice(0, 2) // Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"].
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( var i = 0; i < arr.length; i++){
  if ( arr[i] === 5) {
    arr.splice(i, 1); 
  }
}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 0];
for( var i = 0; i < arr.length; i++){
  if ( arr[i] === 5) {
    arr.splice(i, 1); i--; 
  }
}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
// //////////////////////////////////////
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]
// //////////////////////////////////////
var evens = _.remove(array, function(n) {
  return n % 2 === 0;
});
console.log(array);// => [1, 3]console.log(evens);// => [2, 4]
// //////////////////////////////////////
function arrayRemove(arr, value) {
  return arr.filter(function(ele){
    return ele != value; });
}
var result = arrayRemove(array, 6);// result = [1, 2, 3, 4, 5, 7, 8, 9, 0]
// //////////////////////////////////////
var ar = [1, 2, 3, 4, 5, 6];delete ar[4]; // delete element with index 4console.log( ar ); // [1, 2, 3, 4, undefined, 6]alert( ar ); // 1,2,3,4,,6
-1
0

vylúčiť vales z poľa v js

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

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