Policies & Guidelines

SELECT_BY_KEY

Find objects from an array by a key

Description

Finds multiple objects from the given array by a key-value pair.

For example, given Key "age" and Value 20, will return an array containing all objects in the array whose age property is 20, or an empty array if no matching objects were found.

Function signature

SELECT_BY_KEY(array: Array, key: String, value: ) => Array

Parameter

Description

Array

Array to find objects in

Key

Key whose value to match

Value

Value to match

Examples

array1 = [{"a":3},{"a":2},{"a":2,"b":3}]
array2 = [{"a":3},{"a":{"b":10}}]
array = [{"a":3},{"a":2}]

Formula

Return value

SELECT_BY_KEY(array1, "a", 2)

[{"a":2},{"a":2,"b":3}]

SELECT_BY_KEY(array2, "a", { b: 10 })

[{"a":{"b":10}}]

SELECT_BY_KEY(array, "a", 4)

[]