【发布时间】: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