JS 检查 object/array 的所有最高值
JS checking object/array for ALL highest values
我知道有 Math.max()
、reduce()
,甚至还有 for 循环:
var array = [1 , 2 , 3 , 6 , 12 , 13 , 17 , 3];
var biggest = 0;
for (var i = 0; i < array.length; i++)
{
if(biggest < array[i])
{
biggest = array[i];
}
}
console.log(biggest);
但我需要从 object/array 中获取每个最高元素。例如,我的测量值很少:
var object = {m1 : 100 , m2 : 200, m3: 150, m4 : 200, m5 : 100};
所以我需要从这个对象中获取 m2
和 m4
具有最高值并且值为 200 的信息。
我的想法是复制对象(必须保存原始对象以供进一步检查),获取最高值 - 保存它,将其从对象中删除。我试图通过从对象中删除每个对象直到对象不再具有 200 的值(在本例中)来找到所有其余 key:value
对具有最高分数。
这是一个好方法吗?也许我可以做一些更好的事情,或者也许有一些内置的 JS 功能最快且合成更好?
要得到highest
和值最高的对象,可以用值来匹配。如果它更大,那么您可以替换该值。
您还需要维护包含对象的字典,即 dict
。
在 dict
中插入值时,请务必先检查 key
是否已存在于 dict
中。如果它存在,则只需推送该值,否则创建一个包含该值的新数组。
var object = { m1: 100, m2: 200, m3: 150, m4: 200, m5: 100 };
let highest = Number.MIN_VALUE;
const dict = {};
Object.entries(object).forEach(([key, value]) => {
// Replace the stored highest with the value if it is highest
if (value > highest) highest = value;
// Push the key if the value is already exist in dict
if (dict[value]) dict[value].push(key);
else dict[value] = [key]; // else create a new array with the key
});
console.log(highest);
console.log(dict[highest]);
我会这样修改代码:
var object = { m1: 100, m2: 200, m3: 150, m4: 200, m5: 100 };
// start with MIN_VALUE so the first entry is accepted as highest
let highest = Number.MIN_VALUE;
// empty result object to start with
let best = {};
// call this function for each key-value-pair
Object.entries(object).forEach(([key, value]) => {
// if the value is higher than the current maximum,
if (value > highest) {
// save the value and create a new, empty result object
highest = value;
best = {};
}
// only add the key-value-pair to result object if the value
// equals the current maximum
if (highest == value) best[key] = value;
});
console.log(highest);
console.log(best);
我知道有 Math.max()
、reduce()
,甚至还有 for 循环:
var array = [1 , 2 , 3 , 6 , 12 , 13 , 17 , 3];
var biggest = 0;
for (var i = 0; i < array.length; i++)
{
if(biggest < array[i])
{
biggest = array[i];
}
}
console.log(biggest);
但我需要从 object/array 中获取每个最高元素。例如,我的测量值很少:
var object = {m1 : 100 , m2 : 200, m3: 150, m4 : 200, m5 : 100};
所以我需要从这个对象中获取 m2
和 m4
具有最高值并且值为 200 的信息。
我的想法是复制对象(必须保存原始对象以供进一步检查),获取最高值 - 保存它,将其从对象中删除。我试图通过从对象中删除每个对象直到对象不再具有 200 的值(在本例中)来找到所有其余 key:value
对具有最高分数。
这是一个好方法吗?也许我可以做一些更好的事情,或者也许有一些内置的 JS 功能最快且合成更好?
要得到highest
和值最高的对象,可以用值来匹配。如果它更大,那么您可以替换该值。
您还需要维护包含对象的字典,即 dict
。
在 dict
中插入值时,请务必先检查 key
是否已存在于 dict
中。如果它存在,则只需推送该值,否则创建一个包含该值的新数组。
var object = { m1: 100, m2: 200, m3: 150, m4: 200, m5: 100 };
let highest = Number.MIN_VALUE;
const dict = {};
Object.entries(object).forEach(([key, value]) => {
// Replace the stored highest with the value if it is highest
if (value > highest) highest = value;
// Push the key if the value is already exist in dict
if (dict[value]) dict[value].push(key);
else dict[value] = [key]; // else create a new array with the key
});
console.log(highest);
console.log(dict[highest]);
我会这样修改代码:
var object = { m1: 100, m2: 200, m3: 150, m4: 200, m5: 100 };
// start with MIN_VALUE so the first entry is accepted as highest
let highest = Number.MIN_VALUE;
// empty result object to start with
let best = {};
// call this function for each key-value-pair
Object.entries(object).forEach(([key, value]) => {
// if the value is higher than the current maximum,
if (value > highest) {
// save the value and create a new, empty result object
highest = value;
best = {};
}
// only add the key-value-pair to result object if the value
// equals the current maximum
if (highest == value) best[key] = value;
});
console.log(highest);
console.log(best);