js uses the splice method to delete array elements that may cause problems

The splice() method modifies an array by removing or replacing existing elements or adding new elements in place, and returns the modified content as an array. This method mutates the original array.

What problems will be caused by JavaScript traversing the array and deleting the elements of the array that meet certain conditions through the splice method?

cause problems

When using splicethe method to remove elements from a JavaScript array, several issues may arise:

  1. Changed the length and index of the original array

Using splicethe method to delete elements in the array actually directly modifies the original array, thereby changing the length and index of the array. If the subsequent code depends on the length and index of the original array, errors may occur.

  1. Affects the correctness of the loop

When looping through the array, if you use splicethe method to delete elements, the length and index of the array will be changed, which may cause a loop error or miss some elements. Especially when using fora loop , the value range and step size of the loop variable are calculated based on the length and index of the array. If these values ​​change, it may cause a loop error.

  1. may cause performance issues

Using splicethe method to delete elements in the array will directly modify the original array, causing all elements to move forward, and the delete operation itself is time-consuming, which may cause performance problems.

  1. May cause accidental deletion

When using splicethe method to delete an element in an array, if the index of the deleted element is not calculated correctly, other elements may be accidentally deleted. For example, when deleting an element while traversing an array, if the index of the element is not calculated correctly, the wrong element may be deleted, causing a program error.

  1. Nested loops can lead to unexpected behavior

Unexpected behavior may occur when using nested loops to iterate over an array and remove elements using splicethe method . Because splicethe method modifies the array directly, this affects the indices of the remaining elements. This may cause elements to be skipped or processed multiple times.

  1. Inefficiency when dealing with large arrays

As mentioned earlier, splicethe method will move all elements after the deleted element forward to fill the gap. For large arrays, this can be inefficient. Especially when removing elements from the beginning of the array, since all remaining elements need to be moved forward, this can become a performance bottleneck.

  1. may be difficult to understand

Using splicethe method to delete elements in an array can make the code difficult to understand. Because splicethe method modifies the original array, this can make it difficult to keep track of what is happening to the data. This can make debugging and maintenance more difficult.

In general, when removing elements from an array in JavaScript, use splicethe method with caution. While it can be a useful tool in some cases, it is generally safer and more efficient to filteruse mapalternatives such as or to create a new array containing the required elements.

code example

  1. problem code
const nestArr = [
  { sid: 0, stype: "01" },
  { sid: 1, stype: "02" },
  { sid: 2, stype: "03" },
  { sid: 3, stype: "04" },
];

const ArrA = [
  {
    id: 0,
    type: "01",
    nestArr: [...nestArr],
  },
  {
    id: 1,
    type: "02",
    nestArr: [...nestArr],
  },
  {
    id: 2,
    type: "04",
    nestArr: [...nestArr],
  },
  {
    id: 3,
    type: undefined,
    nestArr: [...nestArr],
  },
];

const forSpliceNameArr = ["01", "02", "04", undefined];

function trySplice(pForSpliceNameArr) {
  ArrA.map((ge) => {
    ge.nestArr = [...nestArr];
    ge.nestArr.map((fe, idx) => {
      pForSpliceNameArr.map((ee) => {
        console.log("ge", ge);
        console.log("fe", fe);
        console.log("ee", ee);
        if (ee && fe.stype === ee && ge.type !== ee) {
          ge.nestArr.splice(idx, 1);
        }
      });
    });
  });
  console.log("ArrA", ArrA);
}

trySplice(forSpliceNameArr);

 ArrAA printout of:

 

 

As can be seen from the figure above, ArrAthe nested array attributes in the third and fourth object elements of the array nestArrrespectively have more array elements stypewith attributes of 02, which is not the expected result.

The above problem code is to develop multiple drop-down boxes, the data sources of the drop-down options are the same, but the options of each drop-down box need to be mutually exclusive (that is, after a drop-down box selects an option, other drop-down boxes cannot select this option again) function encountered.

  1. correct code
const nestArr = [
  { sid: 0, stype: "01" },
  { sid: 1, stype: "02" },
  { sid: 2, stype: "03" },
  { sid: 3, stype: "04" },
];

const ArrA = [
  {
    id: 0,
    type: "01",
    nestArr: [...nestArr],
  },
  {
    id: 1,
    type: "02",
    nestArr: [...nestArr],
  },
  {
    id: 2,
    type: "04",
    nestArr: [...nestArr],
  },
  {
    id: 3,
    type: undefined,
    nestArr: [...nestArr],
  },
];

const forFilterNameArr = ["01", "02", "04", undefined];

function tryFilter(pForFilterNameArr) {
  ArrA.map((ge) => {
    ge.nestArr = [...nestArr];
    const forDelArr = [];
    ge.nestArr.map((fe) => {
      pForFilterNameArr.map((ee) => {
        console.log("ge", ge);
        console.log("fe", fe);
        console.log("ee", ee);
        if (ee && fe.stype === ee && ge.type !== ee) {
          forDelArr.push(fe.stype);
        }
      });
    });
    ge.nestArr = ge.nestArr.filter(
      (item) => forDelArr.join(",").indexOf(item.stype) === -1
    );
  });
  console.log("ArrA", ArrA);
}

tryFilter(forFilterNameArr);

 ArrAA printout of:

 

 

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/130467413