Ae Leung
Ae Leung

Reputation: 378

How could I update certain value when my array is being added a new element

I have an array of products along with production years, there is an item to keep track of the longest production year as well

When I add a new product, how could I sync with the latest longest production year?

Below are my code.

const bucket = {
  longestYear: 2060,
  Category_A: [
    { productName: "toothpaste", productionYear: 2030 },
    { productName: "shampo", productionYear: 2040 },
  ],
  Category_B: [
    { productName: "Macbook", productionYear: 2025 },
    { productName: "Micosoft_computer", productionYear: 2060 },
  ],
};

I have a variable to keep track of the longest production years among all products. And my add new elements function.

const addProduct = (bucket,category,newProduct) => {

bucket[category].push(newProduct)
}

 addProduct(bucket,'Category_B',{ productName: "ASUS", productionYear: 2070 })
 console.log(bucket);

In addition to this, how could I also deal with the cases of delete element, while sync with longest year

Upvotes: 0

Views: 50

Answers (3)

Chris G
Chris G

Reputation: 2110

Everytime you push a new object, you compare the productionYear with what you already have in longestYear I added this

if(newProduct.productionYear > bucket.longestYear){
   bucket.longestYear=newProduct.productionYear;
}

so it looks like this

const bucket = {
  longestYear: 2060,
  Category_A: [
    { productName: "toothpaste", productionYear: 2030 },
    { productName: "shampo", productionYear: 2040 },
  ],
  Category_B: [
    { productName: "Macbook", productionYear: 2025 },
    { productName: "Micosoft_computer", productionYear: 2060 },
  ],
};

const addProduct = (bucket,category,newProduct) => {
    if(newProduct.productionYear > bucket.longestYear){
       bucket.longestYear=newProduct.productionYear;
    }
    bucket[category].push(newProduct)
}

 addProduct(bucket,'Category_B',{ productName: "ASUS", productionYear: 2070 })
 console.log(bucket);

Upvotes: 0

I-vasilich-I
I-vasilich-I

Reputation: 258

const bucket = {
  longestYear: 2060,
  Category_A: [
    { productName: "toothpaste", productionYear: 2030 },
    { productName: "shampo", productionYear: 2040 },
  ],
  Category_B: [
    { productName: "Macbook", productionYear: 2025 },
    { productName: "Micosoft_computer", productionYear: 2060 },
  ],
};

const addProduct = (bucket,category,newProduct) => {
bucket.longestYear = Math.max(bucket.longestYear, newProduct.productionYear)
bucket[category].push(newProduct)
}

 addProduct(bucket,'Category_B',{ productName: "ASUS", productionYear: 2070 })
 console.log(bucket);

Upvotes: 1

Andrew Parks
Andrew Parks

Reputation: 8107

const bucket = {
  longestYear: 2060,
  Category_A: [
    { productName: "toothpaste", productionYear: 2030 },
    { productName: "shampo", productionYear: 2040 },
  ],
  Category_B: [
    { productName: "Macbook", productionYear: 2025 },
    { productName: "Micosoft_computer", productionYear: 2060 },
  ],
}

function addProduct(bucket, cat, item) {
  (bucket[cat]??=[]).push(item)
  bucket.longestYear = Object.values(bucket)
    .filter(Array.isArray)
    .flatMap(a=>a.map(i=>i.productionYear)).sort().pop()
}

addProduct(bucket, 'Category_B', { productName: "ASUS", productionYear: 2070 })

console.log(bucket);

Upvotes: 1

Related Questions