Javascript 查找数组中的前k个元素

Javascript 查找数组中的前k个元素,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个数组,格式如下: var series = [[horse,1],[cat,2],[dog,4],[dragon,4],[cow,6]] 为了根据第二个参数找到前3个元素,我对数组进行排序。为此,我使用以下代码: series.sort( function(a,b) { if (a[1] === b[1]) { return 0; } else { return (a[1] < b[1]) ? 1 : -

我有一个数组,格式如下:

var series = [[horse,1],[cat,2],[dog,4],[dragon,4],[cow,6]]
为了根据第二个参数找到前3个元素,我对数组进行排序。为此,我使用以下代码:

 series.sort( function(a,b) {
        if (a[1] === b[1]) {
            return 0;
    }
    else {
         return (a[1] < b[1]) ? 1 : -1;
    }
});
series.sort(函数(a,b){
如果(a[1]==b[1]){
返回0;
}
否则{
返回(a[1]
这很好用。如果我想找到前三名,我可以选择[0,2]。但是,如果第4个值等于第3个值,那么我将忽略它。在这种情况下,如果我要求返回前3个值,那么输出应该是[[horse,1],[cat,2],[dog,4],[dragon,4],因为dragon和dog的值相等(4)。因此,我想知道是否有一些现成的库或有效的算法可以返回前3个值,这并不一定意味着返回前3个元素数组?

只需构建一个列表:

var top = [];
top.push(series[0]);
top.push(series[1]);
for (var i = 2; i < series.length && series[i][1] == series[2][1]; ++i)
  top.push(series[i]);
var-top=[];
顶推(系列[0]);
顶推(系列[1]);
对于(变量i=2;i
概括一下:

功能顶部(系列,k){
var-top=[];
对于(变量i=;i
var系列=[[“马”,1],“猫”,2],“狗”,4],“龙”,4],“牛”,6]]
num=3;
var-arr=[];

对于(var i=0;i,我将创建第二个数组(长度为3)并循环初始数组。当前三项应自动添加到数组中时。然后,当我们循环第一个数组并找到高于最低值的值时,我们移除最低值并将新项放置在新数组中的适当位置

var series = [[horse,1],[cat,2],[dog,4],[dragon,4],[cow,6]];
function top3(a){
  // Create a new Array to hold the top values
  var top = [a[0], a[1], a[2]]; // Initialize it with the first three items
  for(var i=3;i<a.length;i++){
    /* Find the minimum value and its position */
    var min = top[0][1];
    var min_pos = 0;
    for(var e=1;e<3;e++){
      if(top[e][1]<min){
        min = top[e][1];
        min_post = e;
      }
    }
    /* If larger than the top arrays minimum */
    if( a[i][1] > min ){
      /* remove the top arrays min */
      top.splice(min_pos, 1);
    }
    /* Add the new item into the top array */
    top.push(a[i]);
  }
  /* Now our "top" array has the items with the top 3 values, if you want them sorted use a bubble sort here, if not just return "top" */
  bubbleSortByIndex(a, 1); // Sorts by the second item in an array or arrays
  return top;
};
/*
    Bubble Sort Algorythm adapted from http://en.wikipedia.org/wiki/Bubble_sort
*/
function bubbleSortByIndex(a, i){
  var swapped;
  do {
    swapped = false;
    for(var e=1;e<a.length-1;e++){
      if( a[e-1][i] > A[e][i]){
        swapped = true;
        var temp = a[e-1];
        a[e-1] = a[e];
        a[e] = temp
      }
    }
  } while (swapped);
  return a;
}
top3(series);
var系列=[[马,1],[猫,2],[狗,4],[龙,4],[牛,6];
功能top3(a){
//创建一个新数组以保存顶级值
var top=[a[0],a[1],a[2]];//使用前三项初始化它

对于(var i=3;您不能手动检查第三、第四个(等)元素,如果它们的值相等,则将其添加到结果数组中?lol,可能还有1000个元素具有相同的值..我不知道如果您希望低的数字排在第一位,将有多少个元素的排序是向后的。是的,没错。这只是一个示例。为什么您一开始只将
k-1
元素推到列表中?哦,我是ee.第一次通过循环时,
i==k-1
,所以
series[k-1]==
series[i]`,如果
成功,那么
肯定会成功。这看起来不必要的混乱,IMHO。因为列表是排序的(大概;要真正正确地概括这一点,需要重新考虑整个设置),所以很明显,我们总是希望从列表的最前面去掉那么多。不管“值”是什么这些元素中有很多。我理解。我只是想知道为什么第一个循环是
I
而不是
I
@Barmar是的,我同意这很混乱,但这是一个混乱的前提。对于长列表和相对较短的k,最好只对列表进行一次线性传递,而不进行排序。编辑或两次也许会过去
var series = [["horse",1],["cat",2],["dog",4],["dragon",4],["cow",6]]
num = 3;
var arr = [];
for(var i=0; i<series.length; i++)
{
var curr = series[i][1];
var next = series[i][1];
    if(i<num)
    {
     arr.push(series[i]);
    }
    else if(curr==next)
    {
     arr.push(series[i]);
        break;
    }    
}
console.log(arr);
var series = [[horse,1],[cat,2],[dog,4],[dragon,4],[cow,6]];
function top3(a){
  // Create a new Array to hold the top values
  var top = [a[0], a[1], a[2]]; // Initialize it with the first three items
  for(var i=3;i<a.length;i++){
    /* Find the minimum value and its position */
    var min = top[0][1];
    var min_pos = 0;
    for(var e=1;e<3;e++){
      if(top[e][1]<min){
        min = top[e][1];
        min_post = e;
      }
    }
    /* If larger than the top arrays minimum */
    if( a[i][1] > min ){
      /* remove the top arrays min */
      top.splice(min_pos, 1);
    }
    /* Add the new item into the top array */
    top.push(a[i]);
  }
  /* Now our "top" array has the items with the top 3 values, if you want them sorted use a bubble sort here, if not just return "top" */
  bubbleSortByIndex(a, 1); // Sorts by the second item in an array or arrays
  return top;
};
/*
    Bubble Sort Algorythm adapted from http://en.wikipedia.org/wiki/Bubble_sort
*/
function bubbleSortByIndex(a, i){
  var swapped;
  do {
    swapped = false;
    for(var e=1;e<a.length-1;e++){
      if( a[e-1][i] > A[e][i]){
        swapped = true;
        var temp = a[e-1];
        a[e-1] = a[e];
        a[e] = temp
      }
    }
  } while (swapped);
  return a;
}
top3(series);