Это был бы способ достичь этого (хотя он не очень производительный, потому что он перезаписал бы весь объект, если текущее проверенное значение выше, чем предыдущее).ПОЖАЛУЙСТА, ОБРАТИТЕ ВНИМАНИЕ: я изменил ваш идентификатор на "Scoremaximum", потому что вы действительно не должны иметь пробелов в селекторе идентификаторов.
var max = {
score: 0,
id: 0,
firstname: '',
lastname: '',
city: ''
};
$("#tableau tr td:nth-child(5)").each(function(index, elem) {
var current = parseFloat($(elem).text());
if(current > max.score) {
max.score = current;
max.id = $("#tableau tr td:nth-child(1)")[index].textContent;
max.firstname = $("#tableau tr td:nth-child(2)")[index].textContent;
max.lastname = $("#tableau tr td:nth-child(3)")[index].textContent;
max.city = $("#tableau tr td:nth-child(4)")[index].textContent;
}
});
$('#scoremaximum').on('click', function() {
alert("score: " + max.score + ", id: " + max.id + ", firstname: " + max.firstname + ", lastname " + max.lastname + ", city: " + max.city);
});
Вы могли бы немного увеличить производительность, если бы вы добавили другое свойство к объектуон отслеживает индекс вне цикла и обновляет его в соответствии с max.score.Затем вы можете запустить функцию обратного вызова после завершения цикла и передать ей max.index.Это будет выглядеть так:
var max = {
score: 0,
index: 0,
id: 0,
firstname: '',
lastname: '',
city: ''
};
$("#tableau tr td:nth-child(5)").each(function(index, elem) {
var current = parseFloat($(elem).text());
if(current > max.score) {
max.score = current;
max.index = index;
}
return callback(max.index);
});
function callback(index) {
max.id = $("#tableau tr td:nth-child(1)")[index].textContent;
max.firstname = $("#tableau tr td:nth-child(2)")[index].textContent;
max.lastname = $("#tableau tr td:nth-child(3)")[index].textContent;
max.city = $("#tableau tr td:nth-child(4)")[index].textContent;
}
$('#scoremaximum').on('click', function() {
alert("score: " + max.score + ", id: " + max.id + ", firstname: " + max.firstname + ", lastname " + max.lastname + ", city: " + max.city);
});
Codepen: https://codepen.io/anon/pen/KYyyKV