简体   繁体   English

如何比较多个对象的值?

[英]How can I compare values from multiple objects?

I want to compare values from objects that I keep in an array.我想比较保存在数组中的对象的值。

I know that I can create new arrays with values from each object but I'm trying to find some way to do it without creating them.我知道我可以使用来自每个对象的值创建新数组,但我试图找到某种方法来做到这一点而不创建它们。

Consider we have such situation:考虑我们有这样的情况:

soldiers[first, second, third]
first{name: John, shooting: 95, combat: 50, tactic: 88}
second{name: Arnold, shooting: 97, combat: 72, tactic: 68}
third{name: William, shooting: 87, combat: 86, tactic: 97}

I'd like to select the best soldier from the provided above - but I can't create one rating (ie average).我想从上面提供的中选择最好的士兵 - 但我不能创建一个评级(即平均)。

There will be some conditions that soldier must fill - for example: at least 60 points in combat (no matter if every other property is 100).士兵必须满足一些条件 - 例如:战斗中至少 60 点(无论其他属性是否为 100)。

So I'm trying to find way to compare multiple properties and return name of just one soldier.所以我试图找到方法来比较多个属性并返回一个士兵的名字。

I'll appreciate every tip.我会很感激每一个提示。 Thanks!谢谢!

I have made you an exmaple with comments. 我给你一个评论的例子。 Let me know if this pushes you in the right direction or if you need any other help. 如果这会将您推向正确的方向,或者您需要任何其他帮助,请告诉我。

 const soldiers = [{ name: "John", shooting: 95, combat: 50, tactic: 88 }, { name: "Arnold", shooting: 97, combat: 72, tactic: 68 }, { name: "William", shooting: 87, combat: 86, tactic: 97 } ]; const filteredSoldiers = soldiers .filter(soldier => soldier.combat > 60) // Find every soldier where combat is higher than 60 .map(soldier => { return { name: soldier.name, average: (soldier.combat + soldier.tactic + soldier.shooting) / 3 }; // map will return an array with the filtered soldiers, and we put their average and their name in there }) .sort((a, b) => b.average - a.average); // Lastly we sort them high to low by their average score console.log( filteredSoldiers.length > 0 ? filteredSoldiers[0].name : 'No soldiers with combat score higher thn 60' ); 

jsfiddle 的jsfiddle

In the filter condition you can of course add more checks. 在过滤条件中,您当然可以添加更多检查。

You need to got through all items and select best value; 你需要完成所有项目并选择最佳价值;

Note that some soldiers can have similar values, that's why values.name is array 请注意,一些士兵可以拥有相似的值,这就是为什么values.name是数组

let a = {
    name: "John", shooting: 95, combat: 50, tactic: 88
};
let b = {
    name: "Arnold", shooting: 97, combat: 72, tactic: 68
};
let c = {
    name: "William", shooting: 87, combat: 86, tactic: 97
};
let soldiers = [a, b, c];
let values = {
    shooting: {
        name: [],
        score: 0
    },
    combat: {
        name: [],
        score: 0
    },
    tactic: {
        name: [],
        score: 0
    }
};

soldiers.map((item) => {
    ['shooting', 'combat', 'tactic'].forEach(name => {
        if (item[name] > values[name].score) {
            values[name].name = [item.name];
            values[name].score = item[name];
        } else if (item[name] === values[name].score) {
            values[name].name.push(item.name);
        }
    });
});

console.log(values);

Considering you have mentioned that not all skills are equally important, I would suggest weighted total as a cumulative metric: 考虑到你已经提到并非所有技能都同等重要,我建议将加权总数作为累积指标:

Following demo implements that approach: 以下演示实现了该方法:

 //src array const soldiers = [ {name: 'John', shooting: 95, combat: 50, tactic: 88}, {name: 'Arnold', shooting: 97, combat: 72, tactic: 68}, {name: 'William', shooting: 87, combat: 86, tactic: 97} ]; //cumulative score const cumulative = soldier => { //weight for each skill const weights = {shooting: 1, combat: 1.5, tactic: 1.8}; //extract skill properties into skills-object ({name, ...skills} = soldier); //iterate through skills and calculate weighted total return Object.entries(skills).reduce((score,item) => score += item[1]*weights[item[0]],0); }; //best performer const bestPerformer = soldiers.reduce((best, soldier) => cummulative(best) > cumulative(soldier) ? best : soldier, soldiers[0]); console.log(bestPerformer); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM