0

I'm learning Javascript ECMA6, but I have a problem, I trying a example from my book and results in Chrome and Safari not equal, my question is my question is whether this will affect developments in the future.

var pizzas = ["Margherita", "Mushroom", "Spinach & Rocket", "Pineapple & Sweetcorn"];
delete pizzas[2]

**Safari**
pizzas; //["Margherita", "Mushroom", 3: "Pineapple & Sweetcorn"]

**Chrome**
pizzas; //(4) ["Margherita", "Mushroom", empty, "Pineapple & Sweetcorn"] 0 : "Margherita" 1 : "Mushroom" 3 : "Pineapple & Sweetcorn" 
length : 4

Version Javascript in Chrome : 6.3.292.46

In Chrome I see empty value in 2 position in Chrome and Safari show a '3' in the next value to detele, this is really important to develop in Javascript?

I think that in Firefox is the same result (other result).

Regards

1 Answer 1

2

delete should not be used on arrays. It does not change array length or re-index the array.

Instead use Array#splice()

pizzas.splice(2,1)

will remove 1 element starting at index=2 and reduce the length of the array by one also

See: Deleting array elements in JavaScript - delete vs splice

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

6 Comments

Perfect! but What is the real purpose to use delete (in my book the authors use this to Removing Values from Arrays)
That sounds like a scary book. Or possibly very very old. Or you have taken it out of context and they are deleting properties of objects in arrays
The book is Novice to Ninja Javascript, do you have a book most updated?
Not me. I've never read a programming book in my life to be honest
But do you have somewhere where I can find good documentation about Javascript and guide me about it? Thanks bro!
|

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.