Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 相互比较包含数组的对象属性以找出差异_Javascript_Arrays_Node.js_Json_Express - Fatal编程技术网

Javascript 相互比较包含数组的对象属性以找出差异

Javascript 相互比较包含数组的对象属性以找出差异,javascript,arrays,node.js,json,express,Javascript,Arrays,Node.js,Json,Express,问题: 我正在从事一个涉及JSON文件和express/nodejs的小型项目,我被困在包含以下说明的部分: 使用post路由,使用 以下规则:将每个用户的结果转换为简单数组 数字的数量 比较当前用户的分数与当前用户的分数之间的差异 从潜在的匹配中,逐条提问。把差异加起来 计算总差 示例:用户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计算

问题:

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

使用post路由,使用 以下规则:将每个用户的结果转换为简单数组 数字的数量

比较当前用户的分数与当前用户的分数之间的差异 从潜在的匹配中,逐条提问。把差异加起来 计算总差

示例:用户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 );
});
目标

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

研究

我做过研究,大多数例子都涉及到拥有单独的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));
//读取文件并发送到回调

fs.readFileSync('./app/data/friends.json',findCompatibility) 一些指向正确方向的指针:

  • 为了确保差异不是负值,请使用获取差异的绝对值
  • 现在所有的分数都是字符串,用or将它们转换成数字
  • var data=[{“name”:“Mike Jackson”,“photo”:“/app/public/matchPhotos/photo0.jpg”,“scores”:[“3”、“2”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“4”、“5”、“5”、“2”],{“name”:“Jermaine Subia”、“photo”:“/app“/app/public/matchPhotos/photo2.jpg”,“分数”:[“1”、“5”、“3”、“2”、“3”、“1”、“3”、“4”、“3”、“3”、“3”],{“姓名”:“杰米·舒利”、“照片”:“/app/public/matchPhotos/photo3.jpg”,“分数”:[“5”、“3”、“4”、“4”、“4”、“5”、“5”],{“姓名”:“贾斯汀·安德烈斯”、“照片”:“。/app1、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、照片:“/app/public/matchPhotos/photo7.jpg”,“分数:[“4”、“3”、“2”、“2”、“2”、“2”、“2”、“2”、“2”、“1”],{“姓名:”“桑德拉·史密斯”,“照片:”。/app/public/matchPhotos/photo8.jpg”,“分数:[“1”、“2”、“2”、“2”、“4”、“4”、“3”、“3”、“1”],{“姓名:”“凯文哈特”,“照片:”。/app public/matchPhotos/matchPhotos/photo9.jpg”,“分数:“:”", "3", "2", "2", "5", "5", "4", "3" ] } ];
    函数查找兼容性(数据){
    var结果=[];
    for(设i=0;ilog(findCompatibility(data))你到底被困在哪里?这是你在挣扎的逻辑吗?由于不必比较数组,只需在同一索引处减去数字即可。1) 创建人员分数,如图所示。2) 加载JSON文件,如图所示。3) 循环文件中的所有用户。4) 对于文件中的每个人,循环计算分数。从相同索引的用户分数中减去每个人的分数。5) 将这些数字相加,就得到了用户和用户之间的差值。6) 差异最小的人就是匹配项。当你可以在
    .reduce()
    或其他数组循环中
    parseInt()
    时,你为什么要使用
    parseInt(eval())
    !我正在努力将其合并到我的代码中,但我必须弄清楚为什么它在包含user1.scores.length的for循环中给我一个错误,它正在获取undefined的cannot read属性。我将在上面添加更新,以便您可以看到。欢迎@JermaineSubia。此错误表示数据中的一个对象不是未定义的,就是没有得分属性。@JermaineSubia-在函数中的代码中,在
    handleFile()
    语句之后调用
    findCompatibility(obj)
    。您应该将解析后的对象
    obj
    传递给函数。非常感谢现在我只需写出代码,就可以打印出完美匹配。
    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));