Policies & Guidelines

BUILD_OBJECT

Build an object from an array of key-value objects

Description

Given an array of objects and the names of two properties that the objects in the array have, constructs a dictionary object.

The algorithm runs as follows: start with an empty result object. For every object in the source array, pick the value of the Key value property, and assign it to the result object under a property whose name is the value of Key name property.

If multiple objects in the array have the same value for their Key name property, the dictionary will have the value of the Value name property of the last object in the array.

Function signature

BUILD_OBJECT(array: Array, keyname: String, valuename: String) => Object

Parameter

Description

Array

The array of objects from which the object will be built

Key name

Property name in the source array object whose value to use as dictionary object key

Value name

Property name in the source array object whose value to use as dictionary object value

Examples

array = [{"id":"abc","label":"ABC label"},{"id":"def","label":"DEF label"}]
animals = [
{
"name": "cat",
"age": 2,
"height": 0.4
},
{
"name": "dog",
"age": 3,
"height": 0.6
},
{
"name": "mouse",
"age": 1,
"height": 0.1
}
]

Formula

Return value

BUILD_OBJECT(array, "id", "label")

{"abc":"ABC label","def":"DEF label"}

BUILD_OBJECT(animals, "name", "age")

{"cat":2,"dog":3,"mouse":1}

BUILD_OBJECT(animals, "name", "height")

{"cat":0.4,"dog":0.6,"mouse":0.1}