Untitled

 avatar
unknown
javascript
4 years ago
737 B
5
Indexable
const newFilters = [];

products.forEach((product) => {
  filters.forEach((filter) => {
    const newFilter = newFilters.find(f => f.name === filter.name);
    if(newFilter) {
      if(!newFilter.availableFilters.includes(product[filter.name])) {
        newFilter.availableFilters.push(product[filter.name]);
      }
    } else {
      // clear filter object
      const foundFilter = filters.find((f) => f.name === filter.name);
      if(foundFilter) {
        newFilters.push({
          name: foundFilter.name,
          title: foundFilter.title,
          availableFilters: [product[filter.name]],
          _id: foundFilter._id,
          objectType: foundFilter.objectType
        });
      }
    }
  });
})
Editor is loading...