如何比较多个对象的值?

时间:2019-04-19 12:01:37

标签: javascript arrays object comparison

我想比较保存在数组中的对象中的值。

我知道我可以使用每个对象的值创建新数组,但是我正在尝试找到某种方法来创建它们而不创建它们。

考虑到这种情况:

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}

我想从上面提供的中选择最好的士兵-但我无法创建一个等级(即平均)。

士兵必须满足一些条件-例如:战斗中至少需要60点(无论其他财产是否为100)。

所以我试图找到一种方法来比较多个属性并返回一名士兵的姓名。

我将不胜感激。谢谢!

3 个答案:

答案 0 :(得分:0)

我让您赞叹不已。让我知道这是否将您推向正确的方向,或者您是否需要其他帮助。

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

在过滤条件下,您当然可以添加更多检查。

答案 1 :(得分:0)

您需要浏览所有项目并选择最佳价值;

请注意,某些士兵可能具有相似的值,这就是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);

答案 2 :(得分:0)

考虑到您提到并非所有技能都同样重要,我建议将加权总和作为累积指标:

按照演示执行该方法:

//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);