“Filtre de produit générique javascript” Réponses codées

Filtre de produit générique javascript

const compare = r => l => (typeof l === "object" ? contains(r)(l) : l === r);
const contains = r => l =>
  Object.keys(r).every(k => l.hasOwnProperty(k) && compare(r[k])(l[k]));

var list = [
  {
    name: "Alice",
    address: {
      zip: "10000"
    }
  },
  {
    name: "Bob",
    address: {
      zip: "20000"
    }
  },
  {
    name: "Charlie",
    address: {
      zip: "30000"
    }
  }
];

var filter = {
  address: { zip: "10000" }
};

console.log(list.filter(contains(filter)));
// [{"name":"Alice","address":{"zip":"10000"}}]
Panicky Puma

Filtre de produit générique javascript

filterList(): void {
    let searchtext = this.state.currentSearchBoxValue;
    let searchProperties = this.state.searchBoxOptions.searchProperties;

    let list = this.props.list;

    if(searchtext != undefined && searchtext != '' && searchtext != null) 
    {    
        /* Call filter to filter items that match search criteria */
        const filteredList = list.filter(item => {

            /* Search for any property where the some() criteria is true for 
               current item */
            return searchProperties.some(searchProperty => {

                /* If item value on current searchProperty key includes searchText, 
                   return true. This means some() will return true, and
                   the "item" will be included in filteredList */
                const itemPropertyValue = item[ searchProperty ];

                return itemPropertyValue.includes(searchtext);
            });
        });

        /* Filtered result */
        console.log(filteredList);
    }     
}
Panicky Puma

Réponses similaires à “Filtre de produit générique javascript”

Questions similaires à “Filtre de produit générique javascript”

Plus de réponses similaires à “Filtre de produit générique javascript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code