简体   繁体   中英

Using splice in javascript remove all elements from array

I m trying to remove a specific element from my array with a string index but all my elements are removed

var myArray = new Array();

myArray['abc'] = 'abc';
myArray['cde'] = 'cde';
myArray['efg'] = 'efg';

console.log('before splice:');
console.log(myArray);

myArray = myArray.splice('abc',1);
console.log('after splice:');
console.log(myArray);


before splice:
[abc: "abc", cde: "cde", efg: "efg"]
after splice:
[]

doc found on this link [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

doesn't explicit say if index must be an integer or anything else

What you want here is not Arrays but Objects.

var myObj = {};

myObj['abc'] = 'abc';
myObj['cde'] = 'cde';
myObj['efg'] = 'efg';

// and you can then

delete myObj['abc'];

Arrays can only have integer indexes.

var myArray = new Array();

myArray[0] = 'abc';
myArray[1] = 'cde';
myArray[2] = 'efg';

console.log('before splice:');
console.log(myArray);

myArray.splice(myArray.indexOf('abc'),1);
console.log('after splice:');
console.log(myArray);

JavaScript Array being a JavaScript object can have arbitrary attributes. What you are doing in your code is setting 3 attributes, and not pushing elements to array.

console.log('before splice:');
console.log(myArray);
#=> [ abc: 'abc', cde: 'cde', efg: 'efg' ]
console.log(myArray.length);
#=> 0

The attributes are not present when you print the myArray after splice as splice returns a new Array composed of deleted elements, which in this case happens to be an empty array [] as no elements were deleted

Use delete obj[key] to delete a key value pair from an object. Splice is use in case of arrays and in case of object use delete .

var myArray = new Array();

myArray['abc'] = 'abc';
myArray['cde'] = 'cde';
myArray['efg'] = 'efg';

delete myArray.abc;
// or,
delete myArray['abc'];
myArray['abc'] = 'abc';

It doesn't push item to your array. You just created javascript object to your array. In consequences you can't slice you array.

Put myArray to you webbrowser console. You will see that array is empty.

Apart from the incorrect splice syntax, you're trying to use an array when the data structure is an object.

var obj = {};

obj['abc'] = 'abc';
obj['cde'] = 'cde';
obj['efg'] = 'efg';

You can then delete the property straight off the object:

var keyToDelete = 'abc';
delete obj[keyToDelete];

DEMO

If you had an array:

var arr = ['abc', 'cde', 'efg'];

Then you could use splice, for example in an iteration of the array:

var elToDelete = 'cde';
for (var i = arr.length; i >= 0; i--) {
  if (arr[i] === elToDelete) {
     arr.splice(i, 1);
  }
}

Or you could use filter to return a new array that doesn't contain that element:

var elToDelete = 'cde';
arr = arr.filter(function (el) {
  return el !== elToDelete;
});

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM