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 findArrObjs = function (objArr: Array<any>, valueData: AnyType) {
  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;
    }
  }

  return resArr;
};
