如何在javascript中打印百分比超过70%的所有学生姓名? [英] how to print all students name which have percentage more than 70% in javascript?
问题描述
我正在使用 json-rule-engine
.https://www.npmjs.com/package/json-rules-engine我有一个学生名单,其中有姓名和他们的百分比,我还有 business rule
百分比应该大于或等于 70
.所以我想打印所有百分比超过 70 的学生姓名
I am using json-rule-engine
.
https://www.npmjs.com/package/json-rules-engine
I am having a student list which have name and their percentage, Also I have business rule
the percentage should be greater thank or equal to than 70
. so I want to print all students name those have percentage more than 70
这是我的代码https://repl.it/repls/AlienatedLostEntropy#index.js
学生名单
const students = [
{
name:"naveen",
percentage:70
},
{
name:"rajat",
percentage:50
},
{
name:"ravi",
percentage:75
},
{
name:"kaushal",
percentage:64
},
{
name:"piush",
percentage:89
}
]
规则
engine.addRule({
conditions: {
all: [
{
fact: "percentage",
operator: "greaterThanInclusive",
value: 70
}
]
},
onSuccess(){
console.log('on success called')
},
onFailure(){
console.log('on failure called')
},
event: {
type: "message",
params: {
data: "hello-world!"
}
}
});
代码https://repl.it/repls/AlienatedLostEntropy#index.js任何更新
推荐答案
json-rules-engine 模块以不同的格式获取数据.在您的 Repl.it 中,您没有定义任何事实.
The json-rules-engine module takes data in a different format. In your Repl.it you have not defined any facts.
事实应该是:
let facts = [
{
name:"naveen",
percentage:70
},
[...]
此外,模块本身似乎并不处理一系列事实.您必须对其进行调整才能实现这一目标.这可以通过以下方式完成:
Also, the module itself doesn't seem to process an array of facts. You have to adapt it to achieve this. This can be done with:
facts.forEach((fact) => {
engine
.run(fact)
[...]
最后,在almanac
中找到学生数据.您可以通过以下方式获取这些值:results.almanac.factMap.get('[name|percentage|age|school|etc]').value
Finally, the student data is found inside the almanac
. You can get these values with: results.almanac.factMap.get('[name|percentage|age|school|etc]').value
这是更新的 Repl.it:https://repl.it/@adelriosantiago/json-规则示例
Here is the updated Repl.it: https://repl.it/@adelriosantiago/json-rules-example
这篇关于如何在javascript中打印百分比超过70%的所有学生姓名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!