“JS otrzyma sumę po kluczu” Kod odpowiedzi

Wartości tablicy sumy JavaScript według klucza

var array = [
  {name: "Peter", age: 43},
  {name: "John", age: 32},
  {name: "Jake", age: 21}
];

array.reduce(function(sum, current) {
  return sum + current.age;
}, 0); // 43 + 32 + 21 = 96
TC5550

JS otrzyma sumę po kluczu

var array = [
  {item: "Coffee", price: 4},
  {item: "Brownie", price: 5},
];

const getSumByKey = (arr, key) => {
  return arr.reduce((accumulator, current) => accumulator + Number(current[key]), 0)
}

const total = getSumByKey(array, 'price') // 9
Defiant Dragonfly

Odpowiedzi podobne do “JS otrzyma sumę po kluczu”

Pytania podobne do “JS otrzyma sumę po kluczu”

Więcej pokrewnych odpowiedzi na “JS otrzyma sumę po kluczu” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu