在javascript中找到对象数组的3个最大值

时间:2021-07-20 16:51:25

I would find the 3 largest values ​​of an object array in javascript.

我会在javascript中找到对象数组的3个最大值。

Here is my array of objects :

这是我的对象数组:

data = [
{"name" : "Ariana" , "score" : "130"},
{"name" : "Iggy" , "score" : "270"},
{"name" : "Aron" , "score" : "30"},
{"name" : "Josh" , "score" : "20"},
{"name" : "Kevin" , "score" : "10"},
{"name" : "John" , "score" : "80"},
{"name" : "Nicky" , "score" : "45"}]

The array who i search :

我搜索的数组:

dataTop3 = [
{"name" : "Ariana" , "score" : "130"},
{"name" : "Iggy" , "score" : "270"},
{"name" : "John" , "score" : "80"}]

I just managed to find the bigger one with this code :

我刚刚设法用这段代码找到了更大的一个:

function value_max(data){
    var max=0;
    for (i in data) if(data[i]>max) max=data[i];
    return max;
}
max=value_max(data);

4 个解决方案

#1


0  

Have you tried something like this?

你尝试过这样的事吗?

while ( data.length > 3) {
    if (data[3] > data[2]) {
        data.splice(2,1);
    } else if (data[3] > data[1]) {
        data.splice(1,1);
    } else if (data[3] > data[0]) {
        data.splice(0,1);
    } else {
        data.splice(3,1);
    }
}
dataTop3 = data;

#2


1  

You could sort a copy of the array and slice the top items.

您可以对数组的副本进行排序并对顶部项进行切片。

var data = [{ name: "Ariana", score: "130" }, { name: "Iggy", score: "270" }, { name: "Aron", score: "30" }, { name: "Josh", score: "20" }, { name: "Kevin", score: "10" }, { name: "John", score: "80" }, { name: "Nicky", score: "45" }],
    top3 = data
        .slice()
        .sort(function (a, b) { return b.score - a.score; })
        .slice(0, 3);
    
console.log(top3);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#3


1  

In your solution you are replacing the value in max in each iteration based on the condition, which in the end will give you only one value, you will need to use an array to return more than one value.

在您的解决方案中,您将根据条件替换每次迭代中的max值,最终只给出一个值,您将需要使用一个数组来返回多个值。

A solution would be to just sort the entire array, using a comparator function to compare score key in each object, and then use top k values as you need.

解决方案是只对整个数组进行排序,使用比较器函数比较每个对象中的得分键,然后根据需要使用前k个值。

var data = [
{"name" : "Ariana" , "score" : "130"},
{"name" : "Iggy" , "score" : "270"},
{"name" : "Aron" , "score" : "30"},
{"name" : "Josh" , "score" : "20"},
{"name" : "Kevin" , "score" : "10"},
{"name" : "John" , "score" : "80"},
{"name" : "Nicky" , "score" : "45"}];


data.sort((x,y) => y.score-x.score);

console.log(data.splice(0,3));

Another solution would be to maintain an array of size 4 and sort it on each iteration and add next element at last position.

另一种解决方案是维护一个大小为4的数组,并在每次迭代时对其进行排序,并在最后一个位置添加下一个元素。

var data = [
{"name" : "Ariana" , "score" : "130"},
{"name" : "Iggy" , "score" : "270"},
{"name" : "Aron" , "score" : "30"},
{"name" : "Josh" , "score" : "20"},
{"name" : "Kevin" , "score" : "10"},
{"name" : "John" , "score" : "80"},
{"name" : "Nicky" , "score" : "45"}];

function value_max(data){
var max=0, vals = [];
for (i in data) {
    if(vals.length === 4){
        vals.sort((x,y) => y.score-x.score);
        vals[3] = data[i]
    }else{
        vals.push(data[i]);
    }
}
vals.sort().pop();
return vals;
}
max=value_max(data);
console.log(max);

#4


0  

If I understand your question correctly, you could first sort the array, based on the score, using the following sort-function:

如果我正确理解您的问题,您可以使用以下排序函数首先根据分数对数组进行排序:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Then, you can easily get the first or last three items, depends if you sort highest first or lowest first. So if you sort highest first:

然后,您可以轻松获得第一个或最后三个项目,具体取决于您排名第一或最低的第一项。所以如果你排在第一位:

sortedArray[0], sortedArray[1], sortedArray[2]

sortedArray [0],sortedArray [1],sortedArray [2]

#1


0  

Have you tried something like this?

你尝试过这样的事吗?

while ( data.length > 3) {
    if (data[3] > data[2]) {
        data.splice(2,1);
    } else if (data[3] > data[1]) {
        data.splice(1,1);
    } else if (data[3] > data[0]) {
        data.splice(0,1);
    } else {
        data.splice(3,1);
    }
}
dataTop3 = data;

#2


1  

You could sort a copy of the array and slice the top items.

您可以对数组的副本进行排序并对顶部项进行切片。

var data = [{ name: "Ariana", score: "130" }, { name: "Iggy", score: "270" }, { name: "Aron", score: "30" }, { name: "Josh", score: "20" }, { name: "Kevin", score: "10" }, { name: "John", score: "80" }, { name: "Nicky", score: "45" }],
    top3 = data
        .slice()
        .sort(function (a, b) { return b.score - a.score; })
        .slice(0, 3);
    
console.log(top3);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#3


1  

In your solution you are replacing the value in max in each iteration based on the condition, which in the end will give you only one value, you will need to use an array to return more than one value.

在您的解决方案中,您将根据条件替换每次迭代中的max值,最终只给出一个值,您将需要使用一个数组来返回多个值。

A solution would be to just sort the entire array, using a comparator function to compare score key in each object, and then use top k values as you need.

解决方案是只对整个数组进行排序,使用比较器函数比较每个对象中的得分键,然后根据需要使用前k个值。

var data = [
{"name" : "Ariana" , "score" : "130"},
{"name" : "Iggy" , "score" : "270"},
{"name" : "Aron" , "score" : "30"},
{"name" : "Josh" , "score" : "20"},
{"name" : "Kevin" , "score" : "10"},
{"name" : "John" , "score" : "80"},
{"name" : "Nicky" , "score" : "45"}];


data.sort((x,y) => y.score-x.score);

console.log(data.splice(0,3));

Another solution would be to maintain an array of size 4 and sort it on each iteration and add next element at last position.

另一种解决方案是维护一个大小为4的数组,并在每次迭代时对其进行排序,并在最后一个位置添加下一个元素。

var data = [
{"name" : "Ariana" , "score" : "130"},
{"name" : "Iggy" , "score" : "270"},
{"name" : "Aron" , "score" : "30"},
{"name" : "Josh" , "score" : "20"},
{"name" : "Kevin" , "score" : "10"},
{"name" : "John" , "score" : "80"},
{"name" : "Nicky" , "score" : "45"}];

function value_max(data){
var max=0, vals = [];
for (i in data) {
    if(vals.length === 4){
        vals.sort((x,y) => y.score-x.score);
        vals[3] = data[i]
    }else{
        vals.push(data[i]);
    }
}
vals.sort().pop();
return vals;
}
max=value_max(data);
console.log(max);

#4


0  

If I understand your question correctly, you could first sort the array, based on the score, using the following sort-function:

如果我正确理解您的问题,您可以使用以下排序函数首先根据分数对数组进行排序:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Then, you can easily get the first or last three items, depends if you sort highest first or lowest first. So if you sort highest first:

然后,您可以轻松获得第一个或最后三个项目,具体取决于您排名第一或最低的第一项。所以如果你排在第一位:

sortedArray[0], sortedArray[1], sortedArray[2]

sortedArray [0],sortedArray [1],sortedArray [2]