1

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

6
  • 3
    Arrays can't have string keys! Commented Dec 20, 2015 at 10:05
  • 1
    @Louy: Yes they can. They can't have string indexes, however. Commented Dec 20, 2015 at 10:11
  • Yeah, I guess "key" is an object term. Commented Dec 20, 2015 at 10:12
  • So many answers already, I'm just wondering though, why the need to mix object property with array? I haven't had the need to mix array with object in my experience. Commented Dec 20, 2015 at 10:17
  • 2
    splice() is not like slice() which doesn't modify the original array. splice() modifies the array, and returns the deleted elements, that's why you have an empty array. Commented Mar 30, 2019 at 7:55

6 Answers 6

5

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

To all of you who came here looking for how to actually remove all the members from an array with splice, the simple answer is:

const arr = [1,2,3,4];
arr.splice(0); //[1,2,3,4] 
// arr = []

splice(startPosition, pcsToRemove)

The first 0 tells splice to start from the very beginning of the array.

Not setting the second parameter will make it default to splice the whole array.

Not answering the authors question here, just answering the title:

Using splice in javascript remove all elements from array

Comments

2

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'];

4 Comments

I think you were using myJsonObject.
That was a typo, not a wrong answer, probably while typing i thought the wrong variable, in this case you should mention it in the comment instead of downvoting the answer. My thought.
Alright. I removed the vote anyway after you corrected it. I think you're right.
Not object literals, just objects.
1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.