Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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_Json - Fatal编程技术网

Javascript 加权对象值并确定胜利者

Javascript 加权对象值并确定胜利者,javascript,json,Javascript,Json,我试图权衡多项选择题的答案,并计算出获胜者。在下面的JSON中,选择答案A两次将得到4分 我如何迭代所有答案,并将它们与考虑到的各自权重进行汇总?我理解决定胜利者背后的逻辑,但简单地计算价值观让我大吃一惊 JSON结构如下所示: { "questions": [ { "answers": [ { "value": "A", "weight

我试图权衡多项选择题的答案,并计算出获胜者。在下面的JSON中,选择答案
A
两次将得到4分

我如何迭代所有答案,并将它们与考虑到的各自权重进行汇总?我理解决定胜利者背后的逻辑,但简单地计算价值观让我大吃一惊

JSON结构如下所示:

{
    "questions": [
        {
            "answers": [
                {
                    "value": "A",
                    "weight": "1"
                },
                {
                    "value": "B",
                    "weight": "2"
                },
                {
                    "value": "C",
                    "weight": "3"
                },
                {
                    "value": "D",
                    "weight": "4"
                }
            ]
        },
        {
            "answers": [
                {
                    "value": "B",
                    "weight": "4"
                },
                {
                    "value": "D",
                    "weight": "1"
                },
                {
                    "value": "A",
                    "weight": "3"
                },
                {
                    "value": "C",
                    "weight": "2"
                }
            ]
        }
    ]
}

下面的例子应该有用

var library = {
    "questions": [
        {
            "answers": [
                {
                    "value": "A",
                    "weight": "1"
                },
                {
                    "value": "B",
                    "weight": "2"
                },
                {
                    "value": "C",
                    "weight": "3"
                },
                {
                    "value": "D",
                    "weight": "4"
                }
            ]
        },
        {
            "answers": [
                {
                    "value": "B",
                    "weight": "4"
                },
                {
                    "value": "D",
                    "weight": "1"
                },
                {
                    "value": "A",
                    "weight": "3"
                },
                {
                    "value": "C",
                    "weight": "2"
                }
            ]
        }
    ]
};


//Some test user answers, can change these to test other answers
var answers = ['B', 'C'];

//initialize total score
var score = 0;

//loop through each user answer
answers.forEach(function(answer, index) {

    //look at the answer in the question library at the same index as 
    //the user answer, and find the answer in that array with value 
    //that  equals user answer, then access the weight of that object, 
    //cast to number, and add to score
    score += Number(library.questions[index].answers.find(function(question) { return question.value === answer }).weight)
});

//score should print 4 if user answers are "B" and "C"
console.log(score);

下面的例子应该有用

var library = {
    "questions": [
        {
            "answers": [
                {
                    "value": "A",
                    "weight": "1"
                },
                {
                    "value": "B",
                    "weight": "2"
                },
                {
                    "value": "C",
                    "weight": "3"
                },
                {
                    "value": "D",
                    "weight": "4"
                }
            ]
        },
        {
            "answers": [
                {
                    "value": "B",
                    "weight": "4"
                },
                {
                    "value": "D",
                    "weight": "1"
                },
                {
                    "value": "A",
                    "weight": "3"
                },
                {
                    "value": "C",
                    "weight": "2"
                }
            ]
        }
    ]
};


//Some test user answers, can change these to test other answers
var answers = ['B', 'C'];

//initialize total score
var score = 0;

//loop through each user answer
answers.forEach(function(answer, index) {

    //look at the answer in the question library at the same index as 
    //the user answer, and find the answer in that array with value 
    //that  equals user answer, then access the weight of that object, 
    //cast to number, and add to score
    score += Number(library.questions[index].answers.find(function(question) { return question.value === answer }).weight)
});

//score should print 4 if user answers are "B" and "C"
console.log(score);

您可以使用以下函数获得答案及其相应权重之和。其中数据是所讨论的对象。看看这个


您可以使用以下函数获得答案及其相应权重之和。其中数据是所讨论的对象。看看这个


你可以很容易地使用。运行下面的示例并阅读注释以获得解释

函数sumObjectArray(a){
//使用reduce迭代数组并保持一个运行总数
返回a.reduce(函数(上一个,当前){
//prev是上一个结果,或作为默认值传递的空对象
//循环浏览当前文档中的所有项目
当前.answers.forEach(功能(项){
var值=项目价值;
var-weight=Number(item.weight);//必须转换为Number以防止字符串连接
//若定义了值,则添加到该值,否则将值设置为当前值
如果(上一个[值])
上一个[值]+=权重;
其他的
prev[值]=重量;
});
//通过reduce()为下一次迭代返回更新的对象
返回上一个;
},{};//将空对象作为prev的默认值传递
}
变量输入=[{
“答案”:[{
“价值”:“A”,
“重量”:“1”
}, {
“价值”:“B”,
“重量”:“2”
}, {
“价值”:“C”,
“重量”:“3”
}, {
“价值”:“D”,
“重量”:“4”
}]
}, {
“答案”:[{
“价值”:“B”,
“重量”:“4”
}, {
“价值”:“D”,
“重量”:“1”
}, {
“价值”:“A”,
“重量”:“3”
}, {
“价值”:“C”,
“重量”:“2”
}]
}]

log(sumObjectArray(输入))您可以很容易地使用。运行下面的示例并阅读注释以获得解释

函数sumObjectArray(a){
//使用reduce迭代数组并保持一个运行总数
返回a.reduce(函数(上一个,当前){
//prev是上一个结果,或作为默认值传递的空对象
//循环浏览当前文档中的所有项目
当前.answers.forEach(功能(项){
var值=项目价值;
var-weight=Number(item.weight);//必须转换为Number以防止字符串连接
//若定义了值,则添加到该值,否则将值设置为当前值
如果(上一个[值])
上一个[值]+=权重;
其他的
prev[值]=重量;
});
//通过reduce()为下一次迭代返回更新的对象
返回上一个;
},{};//将空对象作为prev的默认值传递
}
变量输入=[{
“答案”:[{
“价值”:“A”,
“重量”:“1”
}, {
“价值”:“B”,
“重量”:“2”
}, {
“价值”:“C”,
“重量”:“3”
}, {
“价值”:“D”,
“重量”:“4”
}]
}, {
“答案”:[{
“价值”:“B”,
“重量”:“4”
}, {
“价值”:“D”,
“重量”:“1”
}, {
“价值”:“A”,
“重量”:“3”
}, {
“价值”:“C”,
“重量”:“2”
}]
}]

log(sumObjectArray(输入))
假设您通过名为
userAnswer
的列表跟踪用户的答案。您可以使用
problem
变量(您的问题)上的
reduce
api得出一个总分

reduce
调用中,您还可以使用
find
api来定位其中的应答对象。一旦你得到了答案对象,你所要做的就是将字符串中的权重值转换成一个
数字
,并将其添加到
reduce
的累加器参数中

示例:

var userAnswer = [ "A", "A" ];
var problem = {
    "questions": [
    {
        "answers": [
            {
                "value": "A",
                "weight": "1"
            },
            {
                "value": "B",
                "weight": "2"
            },
            {
                "value": "C",
                "weight": "3"
            },
            {
                "value": "D",
                "weight": "4"
            }
        ]
    },
    {
        "answers": [
            {
                "value": "B",
                "weight": "4"
            },
            {
                "value": "D",
                "weight": "1"
            },
            {
                "value": "A",
                "weight": "3"
            },
            {
                "value": "C",
                "weight": "2"
            }
        ]
    }
  ]
};

let score = problem.questions.reduce((accumulator, question, index) => {
    let score = question.answers.find((answer) => { return (answer.value === userAnswer[index]); });
    return accumulator += Number(score.weight);
}, 0);

console.log(`Total score ${score}`);
输出:

var userAnswer = [ "A", "A" ];
var problem = {
    "questions": [
    {
        "answers": [
            {
                "value": "A",
                "weight": "1"
            },
            {
                "value": "B",
                "weight": "2"
            },
            {
                "value": "C",
                "weight": "3"
            },
            {
                "value": "D",
                "weight": "4"
            }
        ]
    },
    {
        "answers": [
            {
                "value": "B",
                "weight": "4"
            },
            {
                "value": "D",
                "weight": "1"
            },
            {
                "value": "A",
                "weight": "3"
            },
            {
                "value": "C",
                "weight": "2"
            }
        ]
    }
  ]
};

let score = problem.questions.reduce((accumulator, question, index) => {
    let score = question.answers.find((answer) => { return (answer.value === userAnswer[index]); });
    return accumulator += Number(score.weight);
}, 0);

console.log(`Total score ${score}`);
总分4分

如果您感兴趣,您可以在此处参考更多有关
reduce
find
的用法示例:


假设您通过名为
userAnswer
的列表跟踪用户的答案。您可以使用
problem
变量(您的问题)上的
reduce
api得出一个总分

reduce
调用中,您还可以使用
find
api来定位其中的应答对象。一旦你得到了答案对象,你所要做的就是将字符串中的权重值转换成一个
数字
,并将其添加到
reduce
的累加器参数中

示例:

var userAnswer = [ "A", "A" ];
var problem = {
    "questions": [
    {
        "answers": [
            {
                "value": "A",
                "weight": "1"
            },
            {
                "value": "B",
                "weight": "2"
            },
            {
                "value": "C",
                "weight": "3"
            },
            {
                "value": "D",
                "weight": "4"
            }
        ]
    },
    {
        "answers": [
            {
                "value": "B",
                "weight": "4"
            },
            {
                "value": "D",
                "weight": "1"
            },
            {
                "value": "A",
                "weight": "3"
            },
            {
                "value": "C",
                "weight": "2"
            }
        ]
    }
  ]
};

let score = problem.questions.reduce((accumulator, question, index) => {
    let score = question.answers.find((answer) => { return (answer.value === userAnswer[index]); });
    return accumulator += Number(score.weight);
}, 0);

console.log(`Total score ${score}`);
输出:

var userAnswer = [ "A", "A" ];
var problem = {
    "questions": [
    {
        "answers": [
            {
                "value": "A",
                "weight": "1"
            },
            {
                "value": "B",
                "weight": "2"
            },
            {
                "value": "C",
                "weight": "3"
            },
            {
                "value": "D",
                "weight": "4"
            }
        ]
    },
    {
        "answers": [
            {
                "value": "B",
                "weight": "4"
            },
            {
                "value": "D",
                "weight": "1"
            },
            {
                "value": "A",
                "weight": "3"
            },
            {
                "value": "C",
                "weight": "2"
            }
        ]
    }
  ]
};

let score = problem.questions.reduce((accumulator, question, index) => {
    let score = question.answers.find((answer) => { return (answer.value === userAnswer[index]); });
    return accumulator += Number(score.weight);
}, 0);

console.log(`Total score ${score}`);
总分4分

如果您感兴趣,您可以在此处参考更多有关
reduce
find
的用法示例:


谢谢,但我希望这些值与它们的权重相对应——而不是混合和匹配。也许我应该更具体一些。谢谢,但我希望这些值与它们的权重相对应——而不是混合和匹配。也许我应该说得更具体一点,这是完美的。谢谢你这么优雅的代码。这工作完美无瑕。谢谢你这么优雅的代码。