app.js

IBO

by roshan100kar

JavaScript

const listOfProducts = [{
    productName: "TV",
    quantity: 10,
    description: "television"
  },
  {
    productName: "AC",
    quantity: 5,
    description: "air conditioner"
  },
  {
    productName: "TV",
    quantity: 10,
    description: "television"
  },
  {
    productName: "AC",
    quantity: 5,
    description: "air conditioner"
  },
  {
    productName: "FAN",
    quantity: 10,
    description: "Ceiling Fan"
  }
];


//Expected OUPUT-1 --> Unique Product Count

/* ouput1 = {
  "TV": 2,
  "AC": 2,
  "FAN": 1
}; */

//Expected OUTPUT -2 --> Quantity sum for Each Unique Product Name

/* ouput2 =  [{
    productName: "TV",
    quantity: 20,
    description: "television"
  },
  {
    productName: "AC",
    quantity: 10,
    description: "air conditioner"
  },
  {
    productName: "FAN",
    quantity: 10,
     description: "Ceiling Fan"
  }
] */

b = []
for(let a in listOfProducts){
    console.log(listOfProducts[a])
    b.push(listOfProducts[a]['productName'])
}
const uniqueCount = new Set(b)
finallyCount = []

uniqueCount.forEach((key)=>{
    i = 0
   for(let a in listOfProducts){
        if(listOfProducts[a]["productName"]==key){
            i = i+1
            
            d = {
                [key]:i
            }
          
        }
         
    }
    finallyCount.push(d)
} )