从 json 对象过滤值范围

Filtering range of values from json object

我无法从 json 对象中过滤一系列值。

以下是范围数据

const contractAmountRange = [
    { id: '1', min: 0, max: 100000, description: 'Less than 0,000' },
    {
        id: '2',
        min: 100001,
        max: 500000,
        description: '0,001 to 0,000',
    },
    {
        id: '3',
        min: 500000,
        max: 1000000,
        description: '0,000 to ,000,000',
    },
    {
        id: '4',
        min: 1000001,
        max: 5000000,
        description: ',000,001 to ,000,000',
    },
    {
        id: '5',
        min: 5000000,
        description: 'More than ,000,000',
    },
];

如果选择了范围中的任何一个,我需要使用所选范围过滤 json 对象。 json对象的结构如下:

const data = [
    {
        tenderNo: 'ASHQ17HH26334',
        awardedAmt: 400000,
        yearAwarded: 2015,
    },
    {
        tenderNo: 'BFG8765TT14000008',
        awardedAmt: 300000,
        yearAwarded: 2015,
    },
    {
        tenderNo: 'AFSH00ETT14000009',
        awardedAmt: 76071.21,
        yearAwarded: 2015,
    },
];

这就是我现在所在的位置。

数据是json对象

contractAmountRange是可用区间数组

rangeSelected是选中的范围,用id表示

const filterData = (data,contractAmountRange,rangeSelected) => {
    // let maxRange, minRange;
    const rangeMap = {};

    for (const item of contractAmountRange) {
        rangeMap[item.id] = item;
    }

     const filteredData = data.filter((el) => {
            return (
                ?????
            );

find选中的范围,再过滤awardedAmt在找到的minmax之间的元素:

const contractAmountRange = [{
    id: "1",
    min: 0,
    max: 100000,
    description: "Less than 0,000"
  },
  {
    id: "2",
    min: 100001,
    max: 500000,
    description: "0,001 to 0,000",
  },
  {
    id: "3",
    min: 500000,
    max: 1000000,
    description: "0,000 to ,000,000",
  },
  {
    id: "4",
    min: 1000001,
    max: 5000000,
    description: ",000,001 to ,000,000",
  },
  {
    id: "5",
    min: 5000000,
    description: "More than ,000,000",
  },
];

const data = [{
    tenderNo: "ASHQ17HH26334",
    awardedAmt: 400000,
    yearAwarded: 2015,
  },
  {
    tenderNo: "BFG8765TT14000008",
    awardedAmt: 300000,
    yearAwarded: 2015,
  },
  {
    tenderNo: "AFSH00ETT14000009",
    awardedAmt: 76071.21,
    yearAwarded: 2015,
  },
];

const filterData = (data, contractAmountRange, rangeSelected) => {
  const foundRange = contractAmountRange.find((x) => x.id === rangeSelected);
  if (!foundRange) {
    throw new Error("No range found!");
  }
  const {
    min,
    max
  } = foundRange;
  return data.filter(
    ({
      awardedAmt
    }) => awardedAmt >= min && awardedAmt <= max
  );
};

const output = filterData(data, contractAmountRange, "2");
console.log(output);