Skip to content

writetome51/get-array-of-property

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

getArrayOfProperty(
      property: string,
      objects: object[]
): any[]

Returns array of values of property found in each of objects.
property is a string that can include dot-notation ( 'property.subproperty.subsubproperty' ) .

Note: property does not have to be an object key. It can also be an array index.
If getting the values of array indexes, here you need to use dot-notation and not
square braces. Example: '1.0' // instead of [1][0]

Examples

let people = [
    {name:'Tom', age: 20},
    {name:'Ron', age: 40},
    {name:'Hank', age: 70},
];
getArrayOfProperty('name', people);
// --> [ 'Tom', 'Ron', 'Hank' ]


people = [
    {name: {first: 'Tom', last: 'Johnson'}},
    {name: {first: 'Ron', last: 'Thompson'}},
    {name: {first: 'Hank', last: 'Lawrence'}},
];
getArrayOfProperty('name.last', people);
// --> [ 'Johnson', 'Thompson', 'Lawrence' ]


let numbers = [
	[1,2,3,4],
	[5,6,7,8],
	[9,10,11,12]
];
getArrayOfProperty('1', numbers);
// --> [ 2, 6, 10 ]


numbers = [
	[[1,2], [3,4]],
	[[5,6], [7,8]],
	[[9,10], [11,12]]
];
getArrayOfProperty('1.0', numbers);
// --> [ 3, 7, 11 ]

Installation

npm i @writetome51/get-array-of-property

Loading

import {getArrayOfProperty} from '@writetome51/get-array-of-property';

About

Returns array of values of a property found in array of objects

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published