5 回答
TA贡献1872条经验 获得超3个赞
首先映射以获取每个对象的 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)
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。
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);
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);
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"
}
]
添加回答
举报