【问题标题】:Compare Object properties that contain array with each other to find difference比较包含数组的对象属性以找出差异
【发布时间】:2020-01-25 20:45:59
【问题描述】:

问题:

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

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

比较当前用户的分数与那些分数之间的差异 从潜在的匹配,一个问题一个问题。将差异加起来 计算总差值。

示例:用户 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):

这是我用于此的代码部分:

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

【问题讨论】:

  • 你到底卡在哪里了?这是你正在努力的逻辑吗?由于您不必比较数组,只需减去相同索引处的数字即可。 1) 创建人员分数,如图所示。 2) 加载 JSON 文件,如图所示。 3) 遍历文件中的所有用户。 4)对于文件中的每个人,循环得分。从相同索引的用户分数中减去每个人的分数。 5) 将这些数字相加,您现在就有了人和用户之间的差异。 6) 差距最小的人,就是匹配的人。

标签: javascript arrays node.js json express


【解决方案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));

【讨论】:

  • 当您可以在parseInt() 或其他数组循环中使用parseInt(eval()) 时,为什么还要使用parseInt(eval())
  • @Shilly 我不知道reduce函数,感谢提示,*已修复
【解决方案2】:

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

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

【讨论】:

  • 谢谢!我正在努力将它合并到我的代码中,但我必须弄清楚为什么它在包含 user1.scores.length 的 for 循环中给我一个错误,它正在获取无法读取未定义的属性。我将在上面添加更新,这样你就可以见。
  • 不客气@JermaineSubia。此错误意味着您的数据中的对象之一是 undefined 或没有 scores 属性。
  • @JermaineSubia - 在handleFile() 函数的代码中,在obj = JSON.parse(data) 语句之后调用findCompatibility(obj)。您应该将解析后的对象 obj 传递给函数。
  • 非常感谢,现在我只需写出代码即可打印出完美匹配。
猜你喜欢
  • 1970-01-01
  • 2010-10-31
  • 2016-10-19
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多