javascript
Finding the max/min value of an attribute in an array of objects
//Mapping formated counting
var arr = ['one', 'two', 'three']
arr.map(element => ({ valor : element.length, descricao: element}) );
//result of the code above and look like this array...
let array =[
{
"valor": 3,
"descricao": "one"
},
{
"valor": 3,
"descricao": "twsso"
},
{
"valor": 5,
"descricao": "threexx"
}
];
let maxvalue = Math.max.apply(Math,array.map(function(o){return o.valor;}))
let minvalue = Math.min.apply(Math,array.map(function(o){return o.valor;}))
let objmax = array.find(function(o){ return o.valor == maxvalue; })
let objmin = array.find(function(o){ return o.valor == minvalue; })
console.log("max word value" + JSON.stringify(objmax));
console.log("min word value" + JSON.stringify(objmin));
Finding the max value of an attribute in an array of objects
Was this helpful?
Similar Posts
- Get Top MAX and MIN itens inside array Javascript
- Get total sum of specif attribute inside of array of objects
- Assign min and max values to y-axis in Chart.js
- Parenthesis Validation, Max Depth and Max Breadth - Braze - Full Stack - 12/20/21
- Javascript Code for Sorting array and finding Largest No. in Array
- Find the index of Max value in an array using Javascript
- Sort array of objects by key value in Javascript