“Menghapus elemen array tunggal” Kode Jawaban

Hapus elemen tertentu dari array

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"]
Grepper

Hapus elemen spesifik dari array javascript

const array = [2, 5, 9];
console.log(array);

const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1); // 2nd parameter means remove one item only
}

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

Menghapus elemen array tunggal

// unset() method
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
          // ↑ Key which you want to delete
          
// Note that when you use unset() the array keys won’t change.

// \array_splice() method
$array = [0 => "a", 1 => "b", 2 => "c"];
\array_splice($array, 1, 1);
                   // ↑ Offset which you want to delete
// If you use \array_splice() the keys will automatically be reindexed
calyCoder

Jawaban yang mirip dengan “Menghapus elemen array tunggal”

Pertanyaan yang mirip dengan “Menghapus elemen array tunggal”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya