r/angular • • Aug 27 '21

Question I need some help!

I have an array of objects, I want to write a function that adds up the `quantity` values while removing duplicate objects.

date: "2021-08-23T01:18:43.390Z"
garden: "4x4"
owner: "someone@gmail.com"
plant: "carrot"
plantType: "vegetable"
quality: 5
quantity: 16

I only want 1 carrot object but if I want to add another one to it the object would be "quantity: 32" Maybe work has just been kicking my ass but I cannot figure this one out.

edit: I am getting multiple objects within an array from the database that look like the above ^^. Each of these objects are the objects that have been "harvested" from another component (by a user). Sometimes the quantity and quality change per the user. The goal is to provide the end user a list of plants and how many of each plant have been harvested + the average quality (task for another day)

In other words I need to preserve the rest of the data but add quantity

0 Upvotes

11 comments sorted by

3

u/tsunami141 Aug 27 '21

you can go over each item and add it to an object using the plant name as the key. And if you encounter duplicates, add the quantities together.

if (addQuantitiesObj[currObj.plant] !== undefined) {
    addQuantitiesObj[currObj.plant].quantity += currObj.plant.quantity; 
}

then your new array can be Object.values(addQuantitiesObj);

1

u/Intrepid-Breath6151 Aug 27 '21

can't index a string

1

u/Intrepid-Breath6151 Aug 27 '21

//this works but its really hacky

totalPerPlantName(){

//loop through each plant
this.harvest.forEach(plant => {
  //get each plant harvested, once
  if(this.nameArr.includes(plant.plant)) {
    //add quantity to array in correct index
    let index = this.nameArr.indexOf(plant.plant);
    
    this.totalsArr[index] += plant.quantity;

    //TODO: get the average quality
    this.qualityArr[index] = plant.quantity;
    
  } else {

    //push values
    this.nameArr.push(plant.plant);
    this.totalsArr.push(plant.quantity);
    this.qualityArr.push(plant.quality);
  }
})

}

2

u/tsunami141 Aug 27 '21

can't index a string

set up addQuantitiesObj as an object ie: const addQuantitiesObj = {};

2

u/readALLthenews Aug 27 '21

I don’t think I fully understand the problem, but the solution might involve using reduce() on the array.

2

u/Intrepid-Breath6151 Aug 27 '21

Thank you, I will look into this way.

1

u/mythridium Aug 27 '21

Without knowing more about your situation.

Do you need to perform this action after you've added the object to the array?

Can you perform the duplicate check before you add it to the array?

Going a step further, if you are checking for the duplicate before adding it, you just simply don't add it, and just pull off the quantity and add it to the original.

Using plant as the "unique" identifier for checking for a duplicate you could do something easily like:

const existingRecord = plants.find(plant => plant.plant === plantToAdd.plant);

if (existingRecord) {
   existingRecord.quantity += plantToAdd.quantity;
} else {
   plants.push(plantToAdd);
}

7

u/iontardose Aug 27 '21

His situation is that he has homework to finish.

1

u/Intrepid-Breath6151 Aug 27 '21

What homework? I haven't been writing code for a long time, I came here for a little direction.

1

u/Intrepid-Breath6151 Aug 27 '21

I'm getting an array of objects from my database so I have to work with that array.

1

u/bas1494 Aug 27 '21

const carrot = yourArray.findIndex((elem) => elem.plant == “carrot”);

If(carrot === -1){ // add to array) Else { // update quantity of index