比较包含数组的对象属性以发现差异

时间:2019-09-25 15:41:50

标签: javascript arrays node.js json express

问题:

我正在处理一个涉及JSON文件和express / nodejs的小型项目,并且被困在包含以下说明的部分:

  

使用发布路线,使用确定用户最兼容的朋友   以下规则:将每个用户的结果转换成一个简单的数组   数字。

     

比较当前用户的得分与那些得分之间的差异   从潜在的比赛,一个问题一个问题。加总差异   计算totalDifference。

     

示例:用户1:[5、1、4、4、5、1、2、5、4、1]用户2:[3、2、6、4,   5,1,2,2,4,1]

     

总差:2 +1 + 2 + 3 = 8

     

请记住使用差异的绝对值;没有负面   结果!您的应用应将5-3和3-5计算为2,依此类推。

我能够得到如下所示的结果(提交的数组是所有5的最后一个数组):

results

这是我为此使用的代码部分:

app.post('/surveyResponse', function(req,res){
   let photo = req.body.url;
   let name = req.body.name;
   let travel = req.body.travel;
   let affection = req.body.affection;
   let family = req.body.family;
   let fitness = req.body.fitness;
   let movie = req.body.movie;
   let education = req.body.education;
   let career = req.body.career;
   let marriage = req.body.marriage;
   let children = req.body.children;
   let pets = req.body.pets;
   let sum = 0;
   let obj = {};
   let person = {
       name: name,
       photo: photo,
       scores: [
           travel,
           affection,
           family,
           fitness,
           movie,
           education,
           career,
           marriage,
           children,
           pets
       ]
   }
//finding the sum of all the numbers
for(i in person.scores){
   sum+=Number(person.scores[i]);
}
//form submission results
let score = person.scores;
// Read the file and send to the callback
fs.readFile('./app/data/friends.json', handleFile)
// Write the callback function
function handleFile(err, data) {
   if (err) throw err
   obj = JSON.parse(data)

   for(var key in obj){
       var obj2 = obj[key];
       console.log(obj2.scores);
   }
   //this is the console.log for my form submission array
   console.log(score);
}
//------------------------------------
// result that prints out on the HTML
res.send('Your name is ' + name + ' You Score is ' + sum );
});

目标

目标是找到结果与用户提交的内容之间差异最小的用户。

重新搜索

我已经完成了How to compare each object in an array with each other. When found update the object with a new property How to Subtract Multiple Objects from an Array with Another array 的研究,并且大多数示例都涉及到拥有单独的JSON对象并将它们彼此进行比较,而我发现比较一组JSON对象只是在比较手机数字。我被困在下一步。我只需要一个快速入门/指导。

这是我正在使用的JSON文件:

[
 {
   "name": "Mike Jackson",
   "photo": "./app/public/matchPhotos/photo0.jpg",
   "scores": [
     "3",
     "2",
     "4",
     "3",
     "3",
     "4",
     "4",
     "4",
     "3",
     "4"
   ]
 },
 {
   "name": "Jermaine Subia",
   "photo": "./app/public/matchPhotos/photo1.jpg",
   "scores": [
     "4",
     "4",
     "2",
     "2",
     "4",
     "5",
     "3",
     "4",
     "5",
     "2"
   ]
 },
 {
   "name": "Taji Gibson",
   "photo": "./app/public/matchPhotos/photo2.jpg",
   "scores": [
     "1",
     "5",
     "3",
     "2",
     "3",
     "1",
     "3",
     "4",
     "3",
     "3"
   ]
 },
 {
   "name": "Jamie Schully",
   "photo": "./app/public/matchPhotos/photo3.jpg",
   "scores": [
     "5",
     "3",
     "3",
     "4",
     "2",
     "4",
     "4",
     "5",
     "5",
     "5"
   ]
 },
 {
   "name": "Justin Andres",
   "photo": "./app/public/matchPhotos/photo4.jpg",
   "scores": [
     "2",
     "1",
     "1",
     "1",
     "2",
     "3",
     "2",
     "2",
     "2",
     "4"
   ]
 },
 {
   "name": "Austin Brooks",
   "photo": "./app/public/matchPhotos/photo5.jpg",
   "scores": [
     "2",
     "3",
     "4",
     "2",
     "4",
     "4",
     "4",
     "4",
     "5",
     "4"
   ]
 },
 {
   "name": "Jessica Jones",
   "photo": "./app/public/matchPhotos/photo6.jpg",
   "scores": [
     "4",
     "4",
     "4",
     "4",
     "4",
     "4",
     "4",
     "4",
     "5",
     "4"
   ]
 },
 {
   "name": "Jasmine Love",
   "photo": "./app/public/matchPhotos/photo7.jpg",
   "scores": [
     "4",
     "3",
     "3",
     "2",
     "2",
     "2",
     "2",
     "1",
     "2",
     "1"
   ]
 },
 {
   "name": "Sandra Smith",
   "photo": "./app/public/matchPhotos/photo8.jpg",
   "scores": [
     "1",
     "2",
     "2",
     "2",
     "4",
     "3",
     "4",
     "3",
     "3",
     "1"
   ]
 },
 {
   "name": "Kevin Hart",
   "photo": "./app/public/matchPhotos/photo9.jpg",
   "scores": [
     "5",
     "5",
     "3",
     "3",
     "2",
     "2",
     "5",
     "5",
     "4",
     "3"
   ]
 }
]

更新1

我正在尝试合并以下代码,但不了解为什么仍然出现以下错误:

  

ReferenceError:数据未定义

我认为这与我尝试合并传入数据的方式有关。我获取了代码,并尝试将其翻译为适合我的代码。

// Read the file and send to the callback
fs.readFileSync('./app/data/friends.json', findCompatibility); <---- This is the line I think is causing issues
// Write the callback function
function findCompatibility(data) {
   var results = [];
   for (let i = 0; i < data.length; i++) {
     for (let j = 1; j < data.length - 1; j++) {
       const user1 = data[i];
       const user2 = data[j];
       var difference = 0;
       for (let k = 0; k < user1.scores.length; k++) {
         difference += Math.abs(Number(user1.scores[k]) - Number(user2.scores[k]));
       }
       results.push({"name": user1.name, "friend": user2.name, "difference": difference});
     }
   }
   return results;
  }
  console.log(findCompatibility(data));

2 个答案:

答案 0 :(得分:1)

var arr1 = [1,4,7,88,40];
var arr2 = [1,77,3,45];

function diff(a1, a2){
      var s1 = a1.reduce((red,n) => red+n);
      var s2 = a2.reduce((red,n) => red+n);

      var total = s1 - s2;

      return total >= 0 ? total : -1*total;
} 

console.log(diff(arr2, arr1));

答案 1 :(得分:1)

一些指向您正确方向的指针:

  1. 要确保差异不是负值,请使用Math.abs()获取差异的绝对值。
  2. 现在所有的乐谱都是字符串,使用Number()parseInt()将它们转换为数字。

var data = [ { "name": "Mike Jackson", "photo": "./app/public/matchPhotos/photo0.jpg", "scores": [ "3", "2", "4", "3", "3", "4", "4", "4", "3", "4" ] }, { "name": "Jermaine Subia", "photo": "./app/public/matchPhotos/photo1.jpg", "scores": [ "4", "4", "2", "2", "4", "5", "3", "4", "5", "2" ] }, { "name": "Taji Gibson", "photo": "./app/public/matchPhotos/photo2.jpg", "scores": [ "1", "5", "3", "2", "3", "1", "3", "4", "3", "3" ] }, { "name": "Jamie Schully", "photo": "./app/public/matchPhotos/photo3.jpg", "scores": [ "5", "3", "3", "4", "2", "4", "4", "5", "5", "5" ] }, { "name": "Justin Andres", "photo": "./app/public/matchPhotos/photo4.jpg", "scores": [ "2", "1", "1", "1", "2", "3", "2", "2", "2", "4" ] }, { "name": "Austin Brooks", "photo": "./app/public/matchPhotos/photo5.jpg", "scores": [ "2", "3", "4", "2", "4", "4", "4", "4", "5", "4" ] }, { "name": "Jessica Jones", "photo": "./app/public/matchPhotos/photo6.jpg", "scores": [ "4", "4", "4", "4", "4", "4", "4", "4", "5", "4" ] }, { "name": "Jasmine Love", "photo": "./app/public/matchPhotos/photo7.jpg", "scores": [ "4", "3", "3", "2", "2", "2", "2", "1", "2", "1" ] }, { "name": "Sandra Smith", "photo": "./app/public/matchPhotos/photo8.jpg", "scores": [ "1", "2", "2", "2", "4", "3", "4", "3", "3", "1" ] }, { "name": "Kevin Hart", "photo": "./app/public/matchPhotos/photo9.jpg", "scores": [ "5", "5", "3", "3", "2", "2", "5", "5", "4", "3" ] } ];

function findCompatibility(data) {
  var results = [];

  for (let i = 0; i < data.length; i++) {
    for (let j = 1; j < data.length - 1; j++) {
      const user1 = data[i];
      const user2 = data[j];
      var difference = 0;

      for (let k = 0; k < user1.scores.length; k++) {
        difference += Math.abs(Number(user1.scores[k]) - Number(user2.scores[k]));
      }

      results.push({"name": user1.name, "friend": user2.name, "difference": difference});
    }
  }
  return results;
}

console.log(findCompatibility(data));