Snippets Collections
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogostick'];

    const transportation = data.reduce((total, item) => {

      if(!total[item]){
        total[item] = 0
      }

      total[item]++
      
      return total

    },{})

    console.log(transportation)


//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)
function getObjectDataByKey( field, theObject ){
	// get data
	return field.split('.').reduce((o,i)=> o[i], theObject);
}

// a simple object
const myObject = {
  name: 'John',
  lastname: 'Doe',
  data: {
    birthDate: '01/01/1970',
    placeOfBirth: 'San Francisco'
  }
}

// variable representing a key of the object
const field = 'data.birthDate';

console.log ( getObjectDataByKey ( field , myObject ) );
//will print
//01/01/1970

// also simple key will return correct value
console.log ( getObjectDataByKey ( 'name' , myObject) );
//will print
//John
var total = value.reduce((prev, next)=>{
  	return prev +(next.quantity * this.productMap(next.productId).price)
},0 )
star

Thu Apr 04 2024 01:28:36 GMT+0000 (Coordinated Universal Time)

#javascript #reduce #counting
star

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

#reduce #array #map #filter
star

Sun Sep 12 2021 09:00:05 GMT+0000 (Coordinated Universal Time)

#reduce #typescript

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension