SoFunction
Updated on 2025-02-28

Methods for inserting and deleting elements in arrays in js

Examples are as follows:

/*
   * Delete array elements: (index)
   */
   = function (index) {
    if (isNaN(index) || index>= ) { return false; }
    (index, 1);
  }
  /*
   * Insert array element: (dx)
   */
   = function (index, item) {
    (index, 0, item);
  };

Through the above function, you can handle the actions of moving up and down

if (tag == 2) { //Move up      if (targeitemindex == 0) return; //top      (targeitemindex); //Remove the specified object and reduce the original object length by one      (targeitemindex - 1, targetitem);
    } else if (tag == 3) { //Move down      if (targeitemindex == len - 1) return; //bottom      (targeitemindex); //Remove the specified object and reduce the original object length by one      (targeitemindex + 1, targetitem);
    }

Definition and usage

The splice() method adds/deletes items to/from the array and returns the deleted item.

Note: This method changes the original array.

grammar

(index,howmany,item1,.....,itemX)

parameter describe
index Required. Integer, specify the position where items are added/deleted, and use negative numbers to specify the position from the end of the array.
howmany Required. Number of items to be deleted. If set to 0, the item will not be deleted.
item1, ..., itemX Optional. New items added to the array.

Return value

type describe
Array A new array containing deleted items, if any.

illustrate

The splice() method deletes zero or more elements starting at index and replaces those deleted elements with one or more values ​​declared in the parameter list.

If an element is deleted from arrayObject, an array containing the deleted elements is returned.

The above method of inserting and deleting array elements in js is all the content I share with you. I hope you can give you a reference and I hope you can support me more.