猿问

从 Javascript 中的每一列获取最大值?

我在 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 列获取最大数字的数组?


白衣非少年
浏览 109回答 5
5回答

守着一只汪

首先映射以获取每个对象的 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)

HUWWW

要从您的对象中获取最大值,请尝试以下操作: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。

莫回无

不是 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++){&nbsp; if (dataArray[i].firstValue > maxFirstValue) {maxFirstValue = dataArray[i].firstValue}&nbsp; if (dataArray[i].secondValue > maxSecondValue) {maxSecondValue = dataArray[i].secondValue}}let resultsObject = {firstValue: maxFirstValue, secondValue: maxSecondValue}console.log(resultsObject);

慕哥6287543

function getColumns(object){&nbsp; &nbsp; //columns:&nbsp; &nbsp; const columns = [];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const keys = Object.keys(object[Object.keys(object)[0]]);&nbsp; &nbsp; keys.forEach(key => columns.push([]));&nbsp; &nbsp; /* keys.shift(); //because you don't want the "date" */&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let i = 0; // to move between the arrays of "columns"&nbsp; &nbsp; for(id in object){&nbsp; &nbsp; &nbsp; &nbsp; keys.forEach(key => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i, key, object[id][key]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columns[i].push(object[id][key]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; console.log("-----------");&nbsp; &nbsp; &nbsp; &nbsp; i = 0;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return columns;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function MathMaxes(array){&nbsp; &nbsp; &nbsp; &nbsp; //is a private function for you <3&nbsp; &nbsp; &nbsp; &nbsp; const result = array.map(column=> Math.max(...column));&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }//the testing objectconst data = {&nbsp; &nbsp; 1:{&nbsp; &nbsp; &nbsp; &nbsp; date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Catan: 23432,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Dominion: 2233,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Codenames: 111,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Terraforming Mars": 2344&nbsp; &nbsp; },&nbsp; &nbsp; 2:{&nbsp; &nbsp; &nbsp; &nbsp; date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Catan: 1123,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Dominion: 353,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Codenames: 54645,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Terraforming Mars": 2333&nbsp; &nbsp; },&nbsp; &nbsp; 3:{&nbsp; &nbsp; &nbsp; &nbsp; date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Catan: 34673,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Dominion: 34532,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Codenames: 6457,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Terraforming Mars": 1231&nbsp; &nbsp; }}const columns = getColumns(data);columns.shift(); //because you don't want the datesconst theMaxes = MathMaxes(columns);console.log(theMaxes);

狐的传说

let foo = [{date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)",&nbsp;Catan: 0,&nbsp;Dominion: 1,&nbsp;Codenames: 2,&nbsp;"Terraforming Mars": 3},{date: "Mon Oct 31 2016 19:00:00 GMT-0500 (Central Daylight Time)",&nbsp;Catan: 3,&nbsp;Dominion: 2,&nbsp;Codenames: 1,&nbsp;"Terraforming Mars": 0}];foo = foo.map(column => {&nbsp; &nbsp;let keys = Object.keys(column);&nbsp; &nbsp;let max = -1;&nbsp; &nbsp;let targetKey = '';&nbsp; &nbsp;keys.forEach(( keyName, index ) => {&nbsp; &nbsp; &nbsp; &nbsp;if(index > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(column[keyName] > max){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = column[keyName];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targetKey = keyName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;})&nbsp; &nbsp;return {value: max, key: targetKey}});console.log("foo", foo);请让我知道这可不可以帮你。预期结果[&nbsp;{&nbsp; value: 3,&nbsp; key: "Terraforming Mars"&nbsp;},&nbsp;{&nbsp; value: 3,&nbsp; key: "Catan"&nbsp;}]
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答