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?