我有一个物体{1:2,2:1}。第一个是关键,第二个是值。我想知道哪一个是身高。从示例开始,第一个具有最高值。
我如何使用jquery或js找到它。
我的预期输出应该是 1是最高的,
如果{1:1,2:2} 然后2是最高的。
如果{1:3} 然后1是最高的。
如果{2:3} 然后2是最高的。
答案 0 :(得分:6)
您可以减少条目并返回具有最高键/值对的对,并仅使用该键。
var object = { 1: 2, 3: 3, 5: 2, 6: 7 },
highest = Object.entries(object).reduce((a, b) => a[1] > b[1] ? a : b)[0];
console.log(highest);

答案 1 :(得分:3)
尝试以下
cols = [2, 3, 4, 12]
row = 9
hrs = 0
nhrs = 0
workbook = Workbook() # assuming you imported openpyxl with *, else adjust
for worker in workers:
sheet = workbook.create_sheet('{} {}'.format(worker.name, worker.surename))
for day, hday in itertools.zip_longest(days, holidays):
if day.weekday() <= 4 and day not in holydays:
sheet.cell(row=row, column=cols[0]).value = '08:00'
sheet.cell(row=row, column=cols[1]).value = '16:00'
sheet.cell(row=row, column=cols[2]).value = '8 h'
hrs += 8
row += 1
elif day.weekday() > 4 and day not in holidays:
sheet.cell(column=cols[0], row=row).value = ''
row += 1
elif day in holidays:
sheet.cell(column=cols[3], row=row).value = '8 h'
nhrs += 8
row += 1
sheet.cell(column=cols[2], row=40).value = str(hrs) + ' h'
sheet.cell(column=cols[3], row=40).value = str(nhrs) + ' h'
workbook.save('SPREADSHEET_{}.xlsx'.format(native_month))
print('Done')
&#13;
答案 2 :(得分:0)
let object = {
1:10,
2:2,
12:30,
15:4
};
// Returns array of a given object's property names
let key = Object.keys(object)
// Compare each value
.reduce((a, b) => object[a] > object[b] ? a : b);
答案 3 :(得分:0)
通过迭代对象的每个属性,您也可以使用for
循环找到它。
var obj = {
"1": 1,
"2": 3,
"3": 7,
"4": 6
},
result, final;
for (var key in obj) {
if (result == undefined) {
result = obj[key];
final = key;
} else {
if (obj[key] > result) {
result = obj[key];
final = key;
}
}
}
console.log(final);
答案 4 :(得分:0)
let obj = {1:2, 2:1, 3:55, 4:1, 5:60};
let highest = Object.keys(obj).reduce(function(a, b){ return obj[a] > obj[b] ? a : b });
console.log(highest);