0

我有两个节点列表,一个是<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... 理想情况下它将是函数的参数)。我怎样才能做到这一点?

4

2 回答 2

1

如果我做对了,您可以执行以下操作...

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>

Javascript

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/

于 2013-10-25T16:52:52.503 回答
0

我可能会在这里使用递归

递归函数将是这样的

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

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

于 2013-10-25T17:20:34.047 回答