Skip to main content

extract

The extract() function takes an array of objects and a key as arguments and returns an array containing the values of the specified key from each object in the input array.

Parameters​

ParameterTypeDescription
arrArrayThe array of objects to extract values from.
keystringThe name of the key to extract values for.

Returns​

  • Array: An array with the extracted values.

Throws​

  • Error: If the array is empty or undefined, or if the key is not a string or is an empty string.

Example​

const utils = require('utils-core.js');
const data = [
{ id: 1, name: "sif", age: 30 },
{ id: 2, name: "Jane", age: 25 },
{ id: 3, name: "Bob", age: 40 },
];

const names = utils.arrays.extract(data, "name");

console.log(names); // ["sif", "Jane", "Bob"]

See also​