Chrome Extension

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));

Do you know, you can create and search your own code snippets on Devsheet. Store your code on cloud using devsheet and search using its chrome extension. Is it look cool?

Finding the max value of an attribute in an array of objects

Search Index Data (The code snippet can also be found with below search text)

Finding the max/min value of an attribute in an array of objects
Was this helpful?
0 Comments
Never leave your website again in search of code snippets by installing our chrome extension.