Ramin Melikov
Ramin Melikov

Reputation: 1005

Get max value from each column in Javascript?

I am working in Javascript. I have an array of objects. Each object looks about like this

{
    date: Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time), 
    Catan: 62588, 
    Dominion: 51915, 
    Codenames: 21263, 
    Terraforming Mars: 2148
}

How do I get an array with max numbers from columns 2-5??

Upvotes: 1

Views: 1241

Answers (5)

Mohammad
Mohammad

Reputation: 121

function getColumns(object){
    //columns:
    const columns = [];
    
    const keys = Object.keys(object[Object.keys(object)[0]]);
    keys.forEach(key => columns.push([]));
    /* keys.shift(); //because you don't want the "date" */
    
    let i = 0; // to move between the arrays of "columns"
    for(id in object){
        keys.forEach(key => {
            console.log(i, key, object[id][key]);
            columns[i].push(object[id][key]);
            i++;
        })
        console.log("-----------");
        i = 0;
    }
    
    return columns;
    }
    
    function MathMaxes(array){
        //is a private function for you <3
        const result = array.map(column=> Math.max(...column));
        return result;
    }

//the testing object
const data = {
    1:{
        date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)", 
        Catan: 23432, 
        Dominion: 2233, 
        Codenames: 111, 
        "Terraforming Mars": 2344
    },
    2:{
        date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)", 
        Catan: 1123, 
        Dominion: 353, 
        Codenames: 54645, 
        "Terraforming Mars": 2333
    },
    3:{
        date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)", 
        Catan: 34673, 
        Dominion: 34532, 
        Codenames: 6457, 
        "Terraforming Mars": 1231
    }
}


const columns = getColumns(data);
columns.shift(); //because you don't want the dates
const theMaxes = MathMaxes(columns);
console.log(theMaxes);

Upvotes: 0

naveen ashok
naveen ashok

Reputation: 331

let foo = [{
date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)", 
Catan: 0, 
Dominion: 1, 
Codenames: 2, 
"Terraforming Mars": 3
},
{
date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)", 
Catan: 3, 
Dominion: 2, 
Codenames: 1, 
"Terraforming Mars": 0
}
];
foo = foo.map(column => {
   let keys = Object.keys(column);
   let max = -1;
   let targetKey = '';
   keys.forEach(( keyName, index ) => {
       if(index > 0){
         if(column[keyName] > max){
            max = column[keyName];
            targetKey = keyName;
         }
       }
   })
   return {value: max, key: targetKey}
});
console.log("foo", foo);

Please let me know if this helps.

Expected result

[
 {
  value: 3,
  key: "Terraforming Mars"
 },
 {
  value: 3,
  key: "Catan"
 }
]

Upvotes: 0

Mark Taylor
Mark Taylor

Reputation: 1188

Not 100% sure I understand the requirement but if you're looking for max values across objects held in an array something like this might work.

let dataArray = [
{ID: "object1", firstValue:7, secondValue:1},
{ID: "object2", firstValue:3, secondValue:10}
]

let maxFirstValue = 0;
let maxSecondValue = 0;

for (let i = 0; i< dataArray.length; i++){
  if (dataArray[i].firstValue > maxFirstValue) {maxFirstValue = dataArray[i].firstValue}
  if (dataArray[i].secondValue > maxSecondValue) {maxSecondValue = dataArray[i].secondValue}
}

let resultsObject = {firstValue: maxFirstValue, secondValue: maxSecondValue}

console.log(resultsObject);

Upvotes: 0

dwosk
dwosk

Reputation: 1257

To get the max value from your object try the following:

let foo = {
    date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)", 
    Catan: 62588, 
    Dominion: 51915, 
    Codenames: 21263, 
    "Terraforming Mars": 2148
};
let arr = Object.keys(foo).map(function(key) { return foo[key] });
arr.shift()
let max = Math.max.apply(null, arr);
console.log(max); // prints 62588

Note I had to make some changes to the object as you originally posted since it's not valid javascript.

Fast way to get the min/max values among properties of object

Upvotes: 0

hgb123
hgb123

Reputation: 14891

First you map to get the array of value from col2-5 for each object, then reduce it to find the max for the corresponding column

const data = [
  {
    date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)",
    Catan: 62588,
    Dominion: 51915,
    Codenames: 21263,
    "Terraforming Mars": 2148,
  },
  {
    date: "Mon Oct 31 2016 20:00:00 GMT-0500 (Central Daylight Time)",
    Catan: 9561,
    Dominion: 74152,
    Codenames: 5123,
    "Terraforming Mars": 1078,
  },
  {
    date: "Mon Oct 31 2016 21:00:00 GMT-0500 (Central Daylight Time)",
    Catan: 62588,
    Dominion: 84102,
    Codenames: 96396,
    "Terraforming Mars": 6423,
  },
]

const res = data
  .map((obj) => Object.values(obj).slice(1, 5))
  .reduce((acc, el) => acc.map((max, i) => Math.max(max, el[i])), [0, 0, 0, 0])

console.log(res)

Upvotes: 5

Related Questions