Dynamic Filter - buttons

PHOTO EMBED

Mon Dec 19 2022 23:12:50 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #reduce #array #map #filter



//get list of buttons and make sure there are no duplicates

function displayButtons(){
    const companies = products.reduce((total, item) => {
       total.push(item.company)
       total = [...new Set(total)]
       return total
    },['All'])
 
    const buttons = companies.map((item) => {
       return `<button class="company-btn">${item}</button>`
    }).join('')
 
    buttonsContainer.innerHTML = buttons
 
 }


 // or we could do this...
 const someBtns = ['all',...new Set(products.map((product) => product.company)),];  
  console.log(someBtns)
content_copyCOPY