给数组中多次出现的数据添加不同的标记

给数组中多次出现的数据添加不同的标记_第1张图片

/**
 * @params  取数组中第二次出现的元素之后的数据
 * @returns
 */
export const getElementsAfterSecondOccurrence = (arr, column, targetValue) => {
  let count = 0;
  let secondIndex = -1;

  for (let i = 0; i < arr.length; i++) {
    if (arr[i][column] === targetValue) {
      count++;
      if (count === 2) {
        secondIndex = i;
        break;
      }
    }
  }

  if (secondIndex !== -1) {
    return [secondIndex, arr.slice(secondIndex)];
  } else {
    return [];
  }
};
 // 取出第二次出现的001
  const secondEle = getElementsAfterSecondOccurrence(dealData, 'groupCode', 'A1');
  secondEle[1]?.forEach((element) => {
    element.groupCode = 'B' + element.groupCode.slice(1, element.groupCode.length);
  });
  let secondEleCopy = cloneDeep(secondEle[1]);

  // 取出第三次出现的001
  let thirdEle = [];
  if (secondEleCopy) {
    thirdEle = getElementsAfterSecondOccurrence(secondEleCopy, 'groupCode', 'B1');
    thirdEle[1]?.forEach((element) => {
      element.groupCode = 'C' + element.groupCode.slice(1, element.groupCode.length);
    });
  }
  // 将处理后结果拼接起来
   if (thirdEle[1]?.length > 0) {
      dealChildVal = [...dealData.slice(0, secondEle[0]), ...secondEle[1].slice(0, thirdEle[0]), ...thirdEle[1]];
    } else if (thirdEle[1]?.length == 0 && secondEle[0]) {
      dealChildVal = [...dealData.slice(0, secondEle[0]), ...secondEleCopy];
    } else {
      dealChildVal = dealData;
    }

你可能感兴趣的:(js)