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
Parameter | Type | Description |
---|---|---|
arr | Array | The array of objects to extract values from. |
key | string | The 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"]