Home > Software engineering >  Filter data from JSON
Filter data from JSON

Time:07-16

I am trying to do filter products with input[range] and I get data to filteredValuesSlider and i need to filter them but I am not sure how to do. Need to check from filteredValuesSlider current value and display only products what have smaller value from productsFiltersSlider

In short productsFilterSlider must be checked and must be same like item.id, its another state(productsList) to display product name, and more... So I need to map another state mean productsList, get item.id and check productsFiltersSlider and get data from it where product_id is same like item.id next I need to check filteredValuesSlider where is filter_id same like filter_id from productsFiltersSlider and if value from productsFiltersSlider is lower than value from filteredValuesSlider display it

      { productsList && (
        productsList.map(item => {
          //item.id is like product_id 
          const test = productsFiltersSlider.filter((dT) => dT.product_id == item.id);
          if(test){
            //need some idea how to complete it
          }
        })
    )
  }

Data from first .map item.id is product_id from filteredValuesSlider

this is productsFiltersSlider

0: {id: '1', product_id: '28', filter_id: '26', type: 'slider', value: '60'}
1: {id: '2', product_id: '28', filter_id: '25', type: 'slider', value: '5'}
length: 2

This is filteredValuesSlider

0: {value: '12', filter_id: '25'}
1: {value: '63', filter_id: '26'}
length: 2

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