从 2 个得分最高的节点列表中获取 3 个节点(我自己设置的属性)

Get 3 nodes from 2 NodeLists which have the biggest scores (an attribute that I have put myself)

本文关键字:节点 我自己 设置 属性 获取 列表      更新时间:2023-09-26

我有两个节点列表,一个是<a>元素,另一个是<img>元素。这些节点列表的大小不同

我为每个节点添加了一个称为 score 的属性。现在我有一种方法可以做到这一点:

getImageWithBiggestScore: function(images,anchor_images) {
                var best_scored_image = images[0];
                for (var i = 1; i < images.length; i++) {
                    if (images[i].score > best_scored_image.score) {
                        best_scored_image = images[i];
                    }
                }
                for (var i = 0; i < anchor_images.length; i++) {
                    if (anchor_images[i].score > best_scored_image.score) {
                        best_scored_image = anchor_images[i];
                    }
                }
                return best_scored_image;
            },

这将返回得分最高的节点。 但是,我想获得 3 个节点(或 5,6...理想情况下,它将是函数的参数)具有最高分数。我该怎么做?

如果我

做对了,你可以执行以下操作......

.HTML

<ul id='x'>
    <li data-score='1'></li>
    <li data-score='44'></li>
    <li data-score='2'></li>
    <li data-score='10'></li>
</ul>
<ul id='y'>
    <li data-score='9'></li>
    <li data-score='1'></li>
    <li data-score='1'></li>
    <li data-score='3'></li>
</ul>

爪哇语

var x = Array.prototype.slice.call(document.getElementById('x').children),   // images
    y = Array.prototype.slice.call(document.getElementById('y').children),   // anchor_images
    z = [],             // array that will keep the results
    param = 4,          // number of nodes as a parameter
    sortFunc = function(a, b) { 
        if(typeof a === 'object' && typeof b === 'object') 
            return (b.getAttribute('data-score') * 1) - (a.getAttribute('data-score') * 1);
    }; // sort desc
x.sort(sortFunc);       // order the first array desc
y.sort(sortFunc);       // order the second array desc
// push into z the highest values of each array 
for(var i = 0; i < param; i++) {
    z.push(x[i]);
    z.push(y[i]);
}
// now, z contains the combination of the highest values from x and y
z.sort(sortFunc);   // sort desc the new array 
z = z.splice(0, param); // get the result :)
var result = "";
for(var i = 0; i < z.length; i++) {
    result += z[i].getAttribute('data-score') + ",";
}
document.getElementById('result').innerHTML = result;

您可能需要进行调整才能使其与特定于您的代码的内容一起工作......操作中的代码:http://jsfiddle.net/YHjgY/5/

我可能会在这里使用递归

递归函数是这样的

var arrObjs = new Array();
arrObjs.push("a elements arr obj"); // pushes the complete array of a elements to the first index of arrObjs
arrObjs.push("div elements arr obj"); // 
var value = run(0,0);
function run (index,biggestScore){
    var arr = new Arrray();
    var smallestLength = 999; // could probably write this a better way. Assuming the length would not be more than 999 length
    for(var i=0; i < arrObjs.length;i++){
       if(arrObjs[i].length >= index){
          if((arrObjs[i].length-index) < smallestLength){
              smallestLength = arrObjs[i].length
          }
       arr.push(arrObjs[i]);
       }
    }
    for(var j=0; j < arr.length;j++){
       for(var k=index; k<arr[j].length;k++){
          if(arr[j][k].score>biggestScore){
              biggestScore.score = arr[j][k];
          }
       }
    }
    biggestScore = run(smallestLength,biggestScore);
    return biggestScore;
}

可能会有一些错误。没有测试它;我想你明白它背后的基本思想。如果您有任何问题,请告诉我:)