Home > Software engineering >  Filter data from JSON (Rebuilded task)
Filter data from JSON (Rebuilded task)

Time:07-17

I am trying to make input range filtering but I have poorly made condition if(test.value >= test.current_value && item.id === test.product_id) but I am not sure how to make it good, because I need to add some check only to current filter_id because now its make filtering but to booth filter_id in one so I am getting bad result..

Code to filter

   productsList.map(item => {
    return(
      finTest.map(test => {
        if(test.value >= test.current_value && item.id === test.product_id){
          return(
              <div>
                    <h6>{item.textname}</h6>
                </div>
          )
        }
      })
    )
})

State finTest I have onChange event to change current_value

const [finTest, setFinTest] = React.useState([
    {
      id: "16",
      product_id: "30",
      filter_id: "25",
      type: "slider",
      value: "3",
      current_value: "2"
    },
    {
      id: "17",
      product_id: "30",
      filter_id: "28",
      type: "slider",
      value: "9",
      current_value: "8"
    }
  ]);

Maybe I have this state what can help

const [filterListDataName, setFilterListDataName] = React.useState([
    {
        "id": "25",
        "name": "Celkový počet portů",
        "type": "slider",
        "slider_min": "1",
        "slider_max": "16"
    },
    {
        "id": "28",
        "name": "Minimální odběr",
        "type": "slider",
        "slider_min": "1",
        "slider_max": "10"
    }
]);

CodePudding user response:

Please implement filter this way:-

if(productsFiltersSlider.filter(fd=> df.product_id == item.id).length === 1){
// //need some idea how to complete it
}

CodePudding user response:

if (productList) {
   let products = []
   for(let i in productList){
        itemId = productList[i].id
        for (let j in productFiltersSlider){
            const dtId = productFiltersSlider[j].product_id
            if (itemId === dtId){
                products.push({...productFiltersSlider[j]})
            }
        }
   }

   let finalProducts = []
   for(let i in products){
        productValue = products[i].value
        for (let j in filteredValuesSlider){
            const filteredValue = filteredValuesSlider[j].value
            if (filteredValue < productValue){
                finalProducts.push({...productValueSlider[j]})
            }
        }
   }
 
}
  • Related