通过选择最后10条记录对多维数组进行排序

时间:2018-02-22 18:33:44

标签: javascript

我需要按编号对此数组进行排序,然后我需要从最高到最低选择最后10条记录。

    function compressArray(original) {

      var compressed = [];
      // make a copy of the input array
      var copy = original.slice(0);

      // first loop goes over every element
      for (var i = 0; i < original.length; i++) {

        var myCount = 0;  
        // loop over every element in the copy and see if it's the same
        for (var w = 0; w < copy.length; w++) {
          if (original[i] == copy[w]) {
            // increase amount of times duplicate is found
            myCount++;
            // sets item to undefined
            delete copy[w];
          }
        }

        if (myCount > 1) {
          var a = new Object();
          a.value = original[i];
          a.count = myCount;
          compressed.push(a);
        }
      }

      return compressed;
    };

结果:

 [{ name: 'Peter', number: 166 },
  { name: 'Frank', number: 165 },
  { name: 'Ana', number: 149 },
  { name: 'Robert', number: 150 },
  { name: 'Jordi', number: 162 },
  { name: 'Alan', number: 168 },
  { name: 'Carlos', number: 174 },
  { name: 'Carena', number: 159 },
  { name: 'Bruce', number: 167 },
  { name: 'Edison', number: 159 },
  { name: 'Paloma', number: 145 },
  { name: 'Devoto', number: 162 },
  { name: 'Julia', number: 150 },
  { name: 'Reina', number: 153 },
  { name: 'Karen', number: 163 },
  { name: 'Violet', number: 170 },]

我需要最后10条记录的结果..

['Carlos',       '紫色',       “艾伦,       “布鲁斯”,       '......',       '......',       '......',       '......',       '......',       '......',]

5 个答案:

答案 0 :(得分:1)

.sort.slice(-10).reverse().map()

&#13;
&#13;
var array = [{ name: 'Peter', number: 166 },  { name: 'Frank', number: 165 },  { name: 'Ana', number: 149 },  { name: 'Robert', number: 150 },  { name: 'Jordi', number: 162 },  { name: 'Alan', number: 168 },  { name: 'Carlos', number: 174 },  { name: 'Carena', number: 159 },  { name: 'Bruce', number: 167 },  { name: 'Edison', number: 159 },  { name: 'Paloma', number: 145 },  { name: 'Devoto', number: 162 },  { name: 'Julia', number: 150 },  { name: 'Reina', number: 153 },  { name: 'Karen', number: 163 },  { name: 'Violet', number: 170 }];

var result = array.sort((a, b) => a.number - b.number)
                  .slice(-10)
                  .reverse()
                  .map(n => n.name)

console.log(result);
&#13;
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

要通过减少数字对数组进行排序,请执行以下操作:

array.sort((a, b) => b.number - a.number)

然后,您可以通过切割前10个元素来按递减顺序获取10个最大元素:

array.sort((a, b) => b.number - a.number)
     .slice(0, 10)

<强>段:

&#13;
&#13;
let array =
 [{ name: 'Peter', number: 166 },
  { name: 'Frank', number: 165 },
  { name: 'Ana', number: 149 },
  { name: 'Robert', number: 150 },
  { name: 'Jordi', number: 162 },
  { name: 'Alan', number: 168 },
  { name: 'Carlos', number: 174 },
  { name: 'Carena', number: 159 },
  { name: 'Bruce', number: 167 },
  { name: 'Edison', number: 159 },
  { name: 'Paloma', number: 145 },
  { name: 'Devoto', number: 162 },
  { name: 'Julia', number: 150 },
  { name: 'Reina', number: 153 },
  { name: 'Karen', number: 163 },
  { name: 'Violet', number: 170 }];
  
let sort = array.sort((a, b) => b.number - a.number).slice(0, 10);
 
console.log(sort);
&#13;
&#13;
&#13;

如果您只想 输出中的名称,请将元素映射到其名称:

array.sort((a, b) => b.number - a.number)
     .slice(0, 10)
     .map(e => e.name);

<强>段:

&#13;
&#13;
let array =
 [{ name: 'Peter', number: 166 },
  { name: 'Frank', number: 165 },
  { name: 'Ana', number: 149 },
  { name: 'Robert', number: 150 },
  { name: 'Jordi', number: 162 },
  { name: 'Alan', number: 168 },
  { name: 'Carlos', number: 174 },
  { name: 'Carena', number: 159 },
  { name: 'Bruce', number: 167 },
  { name: 'Edison', number: 159 },
  { name: 'Paloma', number: 145 },
  { name: 'Devoto', number: 162 },
  { name: 'Julia', number: 150 },
  { name: 'Reina', number: 153 },
  { name: 'Karen', number: 163 },
  { name: 'Violet', number: 170 }];
  
let sort = array.sort((a, b) => b.number - a.number)
                .slice(0, 10)
                .map(e => e.name);
 
console.log(sort);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

 const result = original.sort((a, b) => a.number - b.number).slice(-10).reverse();

答案 3 :(得分:0)

有一个更简单的解决方案:

//if duplicates are OK
function compressArray(array) {
    return array
             .sort((a,b) => b.number - a.number )
             .map(result => result.name);
             .slice(0,10)
}

或者,如果重复不正确。先删除它们

答案 4 :(得分:0)

你可以用.sort和.slice

来做

尝试:

arr.sort((a, b) => b.number - a.number);
let new_arr = arr.slice(0, 10);