During the interview, if the examiner asks you to use JavaScript to insert, delete, and,
During the interview, if the examiner asks you to use JavaScript to insert, delete, and replace array elements. If you do not know the method used by Array. prototype. splice, the score may be deducted. Using the built-in splice method of javascript array type, you can insert, delete, and replace array elements with only one line of code.
Method Name: Array. prototype. splice (index, count [, elm1, elm2... n])
Array splice can be used to insert, replace, and delete Array elements. This method directly affects the current array object (different from the. slice (index1, index2) method) and returns the deleted array items.
Parameters:
- Index: the starting subscript of the element in the array.
- Count: number of elements to be deleted or replaced.
- Elems: The items to be inserted into the array.
- Return Value: return the items removed from the group.
Demo: