我有这个示例示例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已下注1,第一个已下注0。

我该如何完成感谢?

最佳答案

 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 );
});

10-07 14:48