gpt4 book ai didi

javascript - 每个函数获取差异数组的最大值

转载 作者:行者123 更新时间:2023-11-30 09:50:05 25 4
gpt4 key购买 nike

我有这个示例 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);

我希望得到这样的结果(赞成票和反对票之间的差异数组):

  [5,5,4,4]

但是在我的页面中我得到了

  [5544,5544,5544,5544]

当我创建这个 fiddle 时,它给出了你看到的结果。

我怎样才能得到我想要的结果 [5,5,4,4] 因为我想进一步选择最高赞成票和最低反对票数

 var maxvoteup = Math.max.apply(Math, maxvoteups);

然后我想获得第一个 5,因为第二个 5 的反对票数为 1,而第一个的反对票数为 0。

我怎样才能完成这个谢谢?

最佳答案

与@Jose 的意图相同,但实现方式不同

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

关于javascript - 每个函数获取差异数组的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36957005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com