Deleting array elements in JavaScript - delete vs splice

后端 未结 27 3722
予麋鹿
予麋鹿 2020-11-21 05:31

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method?

For example:

myArray = [\         


        
27条回答
  •  忘掉有多难
    2020-11-21 06:10

    It's probably also worth mentioning that splice only works on arrays. (Object properties can't be relied on to follow a consistent order.)

    To remove the key-value pair from an object, delete is actually what you want:

    delete myObj.propName;     // , or:
    delete myObj["propName"];  // Equivalent.
    

提交回复
热议问题