identify maximum values within object properties in array
npm install --save max-values
import maxValues from 'max-values';
const input = [{ name: 'Bob', age: 25, skills: { crypto: 1 }},
{ name: 'Some', age: 14, skills: { crypto: 5 }},
{ name: 'Alice', age: 7, skills: { crypto: 10 }}]
maxValues(input, ['age', 'skills.crypto']);
// [{ name: 'Bob', age: 25, skills: { crypto: 1 }, maxAge: true },
// { name: 'Some', age: 14, skills: { crypto: 5 }},
// { name: 'Alice', age: 7, skills: { crypto: 10 }, maxSkillsCrypto: true }]
Return Array
of the almost the same Objects
. New objects will have new maxProp: true
field, if this object has the maximum value for this prop amongst all the objects.
Required
Type: Array
of Objects
Example: [{ age: 16 }, { age: 18 }]
Objects with similar structure.
Required
Type: Array
of props String
Example: ['age', 'skills.cryptography']
Properties for which you want to find maximum values. Nested properties should be defined with dot notation. Resulting name for maxProps is prepended by 'max' and camelcased prop
:
age => maxAge
skills.cryptography => maxSkillsCryptography
MIT © Vladimir Starkov