我有这个示例示例here。
<div class="subchatarea2">
<div class='message_holder user45 chatid28'>
<div class='likescore'>5</div><div class='unlikescore'>0</div>
</div>
<div class='message_holder user46 chatid29'>
<div class='likescore'>6</div><div class='unlikescore'>1</div>
</div>
<div class='message_holder user47 chatid30'>
<div class='likescore'>4</div><div class='unlikescore'>0</div>
</div>
<div class='message_holder user48 chatid31'>
<div class='likescore'>6</div><div class='unlikescore'>2</div>
</div>
</div>
我的js是
var his = $('.subchatarea2 .message_holder');
var maxvoteups = [];
var diff = his.children('.likescore').text()-his.children('.unlikescore').text();
his.each(function() {
maxvoteups.push(diff);
});
var maxvoteup = Math.max.apply(Math, maxvoteups);
alert(maxvoteup);
我希望得到这样的结果(upvote和downvote之间的差异数组):
[5,5,4,4]
但是在我的页面中我得到了
[5544,5544,5544,5544]
当我创造这个小提琴时,它会给你看到的结果。
我怎样才能得到我的愿望结果[5,5,4,4]
,因为我想进一步选择最低投票最低投票数
var maxvoteup = Math.max.apply(Math, maxvoteups);
然后我想获得第一个5
,因为第二个5有downvote 1而第一个有downvote 0。
我怎么能完成这件事呢?
答案 0 :(得分:3)
var his = $('.subchatarea2 .message_holder');
var $votes = his.map(function(i, node){
//first parse the text in the DOM into some data-structure
var $node = $(node);
return {
node: $node,
up: +$node.children('.likescore').text(),
down: +$node.children('.unlikescore').text(),
get diff(){
return this.up - this.down;
}
};
}).sort(function(a, b){
//and sort it by: `diff` desc, `down` asc
return b.diff - a.diff || a.down - b.down;
});
//so your prefered value is at index 0
console.log("maxvoteup", $votes[0]);
console.log("votes", $votes);
答案 1 :(得分:1)
var his = $('.subchatarea2 .message_holder');
var maxvoteups = [];
his.each(function() {
var diff = $(this).children('.likescore').text() - $(this).children('.unlikescore').text();
var downvotes = $(this).children('.unlikescore').text();
maxvoteups.push([diff, downvotes]);
});
现在你有一个这样的清单:
[[5,0], [6,1], [4,0],[6,2]]
是[[totalvotes(total_up - total_down), total_downvotes]]
编辑: 比这更好的是制作一个对象列表:
var his = $('.subchatarea2 .message_holder');
var maxvoteups = [];
his.each(function() {
var diff = $(this).children('.likescore').text() - $(this).children('.unlikescore').text();
var downvotes = $(this).children('.unlikescore').text();
maxvoteups.push({total : diff, down : downvotes});
});
然后是一个比较对象的函数:
function compare(a,b) {
if (a.total > b.total)
return -1;
else if (a.total < b.total)
return 1;
else if (a.down > b.down) // If a has more downvotes the a is under b.
return -1;
else if (a.down < b.down)
return 1;
}
现在按如下顺序排列列表:
maxvoteups.sort(compare);
最终代码:
var his = $('.subchatarea2 .message_holder');
var maxvoteups = [];
his.each(function() {
var diff = $(this).children('.likescore').text() - $(this).children('.unlikescore').text();
var downvotes = $(this).children('.unlikescore').text();
var upvotes = $(this).children('.likescore').text();
var node = $(this);
maxvoteups.push({total : diff, downvotes : downvotes, upvotes : upvotes, node : node});
});
//var maxvoteup = Math.max.apply(Math, maxvoteups);
function compare(a,b) {
if (a.total > b.total)
return -1;
else if (a.total < b.total)
return 1;
else if (a.down < b.down)
return -1;
else if (a.down > b.down)
return 1;
}
maxvoteups.sort(compare);
alert('The max value is : ' + maxvoteups[0].total + ' with ' + maxvoteups[0].upvotes + ' upvotes and ' + maxvoteups[0].downvotes + ' downvotes and is the jquery element ' + maxvoteups[0].node );
maxvoteups.forEach(function(post, index){
alert('The' + (index + 1) + ' value of the sorted list is : ' + post.total + ' with ' + post.upvotes + ' upvotes and ' + post.downvotes + ' downvotes and is the jquery element ' + post.node );
});
答案 2 :(得分:0)
您需要将diff
变量保留在循环中
var his = $('.subchatarea2 .message_holder');
var maxvoteups = [];
var = 0;
his.each(function() {
diff = his.children('.likescore').text()-his.children('.unlikescore').text();
maxvoteups.push(diff);
});
var maxvoteup = Math.max.apply(Math, maxvoteups);
alert(maxvoteups);
答案 3 :(得分:0)
一个简单的JS版本只不过是3行变量。类似的东西;
var his = Array(...document.querySelectorAll('.subchatarea2 .message_holder')),
maxvoteups = his.map( e => e.firstElementChild.innerText - e.lastElementChild.innerText),
maxvoteup = Math.max(...maxvoteups);
document.write("<pre>Array Values: " + maxvoteups + " Max Upvote: " + maxvoteup + "</pre>");
<div class="subchatarea2">
<div class='message_holder user45 chatid28'>
<div class='likescore'>5</div>
<div class='unlikescore'>0</div>
</div>
<div class='message_holder user46 chatid29'>
<div class='likescore'>6</div>
<div class='unlikescore'>1</div>
</div>
<div class='message_holder user47 chatid30'>
<div class='likescore'>4</div>
<div class='unlikescore'>0</div>
</div>
<div class='message_holder user48 chatid31'>
<div class='likescore'>6</div>
<div class='unlikescore'>2</div>
</div>
</div>