亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Javascript 中的每一列獲取最大值?

從 Javascript 中的每一列獲取最大值?

白衣非少年 2023-04-20 16:23:58
我在 Javascript 工作。我有一組對象。每個對象看起來像這樣{    date: Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time),     Catan: 62588,     Dominion: 51915,     Codenames: 21263,     Terraforming Mars: 2148}如何從第 2-5 列獲取最大數字的數組?
查看完整描述

5 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

首先映射以獲取每個對象的 col2-5 值數組,然后減少它以找到相應列的最大值


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)


查看完整回答
反對 回復 2023-04-20
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

要從您的對象中獲取最大值,請嘗試以下操作:


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

請注意,我必須對您最初發布的對象進行一些更改,因為它不是有效的 javascript。

查看完整回答
反對 回復 2023-04-20
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

不是 100% 確定我理解這個要求,但是如果你正在尋找數組中保存的對象的最大值,那么這樣的事情可能會起作用。


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


查看完整回答
反對 回復 2023-04-20
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

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


查看完整回答
反對 回復 2023-04-20
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

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


請讓我知道這可不可以幫你。


預期結果


[

 {

  value: 3,

  key: "Terraforming Mars"

 },

 {

  value: 3,

  key: "Catan"

 }

]


查看完整回答
反對 回復 2023-04-20
  • 5 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號