Policies & Guidelines

SUM_BY_KEY

Calculate the sum of numeric key values of objects

Description

Calculates the sum of all numeric key values of objects in the given array. Returns 0 if the array is empty or all key values are non-numeric.

Function signature

SUM_BY_KEY(objects: Array, keyname: String) => Number

Parameter

Description

Objects

Array of objects

Key name

Name of the key whose values to sum

Examples

products = [{"name":"Duct tape","price":5},{"name":"Swiss army knife","price":60}]
animals = [
{
"name": "dog",
"weight": 123
},
{
"name": "cat",
"weight": 48
},
{
"name": "rat",
"weight": 10
}
]

Formula

Return value

SUM_BY_KEY(products, "price")

65

SUM_BY_KEY(animals, "weight")

181