Sum values with criteria

#1

(5) [{…}, {…}, {…}, {…}, {…}]
0
:
{idPVI: “10”, idPV: “3”, descri: “Portuguesa”, preco: “32”}
1
:
{idPVI: “10”, idPV: “3”, descri: “Portuguesa”, preco: “32”}
2
:
{idPVI: “13”, idPV: “3”, descri: “Calabresa”, preco: “35”}
3
:
{idPVI: “11”, idPV: “5”, descri: “Borda Recheada”, preco: “5”}
4
:
{idPVI: “12”, idPV: “6”, descri: “GG”, preco: “39.99”}

I have this array of objects up, I want to add the values with different criteria.
Type: what has “idPV” = x, pay the highest value and what has “idPV” = y, normal sum!

already tried with “For”, “forEach” but I could not.

#2
// start with 0
let sum = 0;
// loop over your array
array.forEach((i) => {
  // with each loop, add to the sum
  // idPVI is a string, in order to add it, we must convert it to a number
  sum+=parseInt(i.idPVI);
})
#3

Hahaha. This is a perfect demonstration of why const in JavaScript is less than worthless.

#4

plus I would like to add the keys “preco” in different ways…

because that way the sum is equal. Type: sum all and depending on the “idPV” I want you to not add more sum that takes the largest number and being a “idPV” different from that can add normal!