import { AnyType } from 'src/common/types';

function func(data: Array<any>, key: string, value: any) {
  return data.filter((item: any) => {
    return item[key] == value;
  });
}

/**
 * 数组对象删除 改变原数组
 * @param objArr - 原始数组
 * @param valueData - 删除的对象条件{age:18,num:1}
 **/
export const delArrObj = function (objArr: Array<any>, valueData: AnyType) {
  for (let i = 0; i < objArr.length; i++) {
    objArr[i].item_del_id = i;
  }
  const valueDataArr = [] as any[];
  for (const key in valueData) {
    valueDataArr.push({
      key: key,
      value: valueData[key],
    });
  }

  let resArr = [] as any;
  for (let i = 0; i < valueDataArr.length; i++) {
    if (i == 0) {
      const res = func(objArr, valueDataArr[i].key, valueDataArr[i].value);
      resArr = res;
    } else {
      const res = func(resArr, valueDataArr[i].key, valueDataArr[i].value);
      resArr = res;
    }
  }

  for (let i = 0; i < objArr.length; i++) {
    for (let j = 0; j < resArr.length; j++) {
      if (objArr[i].item_del_id == resArr[j].item_del_id) {
        objArr.splice(i, 1);
      }
    }
  }
  for (let i = 0; i < objArr.length; i++) {
    delete objArr[i].item_del_id;
  }
};
