0

I have an array like this:

var myArr= ["zero", "one", "two", "three", "four"];

if I want to access any value in this array I would do something like...

var numberTwo = myArr[2] 

My issue is that I want to delete one item from my array, but not loose the existing number reference... something like this...

myArr.splice(2,1)

So my array will now be...

["zero", "one", "three", "four"]

Is it possible to delete item 2 for example but still be able to access "three" using this code : var numberThree = myArr[3] instead of var numberThree = myArr[2]

4
  • 1
    Set myArr[2] to null? Commented Sep 21, 2017 at 16:58
  • what is the benefit? Commented Sep 21, 2017 at 16:59
  • Why do you want to delete it..? Commented Sep 21, 2017 at 16:59
  • 1
    What exactly are you trying to do here? Why do you need to delete an item from an array but not reindex the array? This seems like a XY problem and if you described the larger issue you are trying to solve, the solution is probably something other than using an array. Maybe what you need is an object? { 0: "zero", 1:"one",... } Commented Sep 21, 2017 at 17:02

3 Answers 3

3

Definitely not. If you remove something from the array, array items are re-indexed to properly feet from 0 to length - 1. Actually it not removes anything, but creates a new array and omit the selected items range.

But if you have some logic with it and it is mandatory, you can set it's value to undefined or null and in your code check the value. If it is undefined or null, consider it is removed from the array.

Sign up to request clarification or add additional context in comments.

Comments

2

You could use the delete operator for deleting an item of an array.

For checking, you could use in operator, because the value of deleted items of the array is undefined, which is not different from a intended value of undefined. To check a sparse item, you could take the in operator.

var array = ["zero", "one", "two", "three", "four"];

delete array[3];

console.log(array[3]);   // undefined
console.log(array[4]);   // 'four'
console.log(3 in array); // false
console.log(1 in array); // true

4 Comments

You could, but you really probably shouldn't
@MattBurland, the task is to keep the index. with splicing, the index moves.
No doubt. But you are definitely abusing an array. This isn't what an array is for.
@MattBurland, abusing is a hash word, i would try to work without, but i do not condemn it.
0

You may use a hash table instead:

var myArr = {0: "zero", 1: "one", 2: "two", 3: "three", 4: "four"};

Then you could delete any element you want:

delete myArr[2];

And then access the elements in the same way:

myArr[3] //=> three
myArr //=> {0: "zero", 1: "one", 3: "three", 4: "four"}

You can use a more readable keys:

var myArr = { "elem0": "zero", "elem1": "one" }

3 Comments

Don't change the shape of the object during the execution. It causes the engines not to optimize the code
This is true only for V8, and it happens when we extend the object and not when we delete a property. What will have an impact on optimization is to delete an array element and not a hash-table entry.
When you delete, new class is generated for the object and it differs from which the V8 expects to work with.

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.