如何打印 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 的学生姓名
这是我的代码
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!"
}
}
});
json-rules-engine 模块采用不同格式的数据。在你的 Repl.it 中你没有定义任何事实。
事实应该是:
let facts = [
{
name:"naveen",
percentage:70
},
[...]
此外,模块本身似乎不处理事实数组。您必须对其进行调整才能实现这一目标。这可以通过以下方式完成:
facts.forEach((fact) => {
engine
.run(fact)
[...]
终于在almanac
里面找到了学生数据。您可以通过以下方式获取这些值:results.almanac.factMap.get('[name|percentage|age|school|etc]').value
这是更新后的 Repl.it:https://repl.it/@adelriosantiago/json-rules-example
我可能提交了一个完全不相关的答案,但这里是。由于 students
对象是一个数组,您可以循环遍历它,然后使用 if else 语句。
for (let i = 0; i < students.length; i++) {
if (students[i].percentage >= 70) {
console.log(students[i].name);
}
}
抱歉,如果这不正确!
这是一个工作示例。
计数 success
和 failed
个案例
const { Engine } = require("json-rules-engine");
let engine = new Engine();
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
}]
},
event: { type: 'procedure_result'}
})
let result = {success_count : 0 , failed_count : 0}
engine.on('success', () => result.success_count++)
.on('failure', () => result.failed_count++)
const getResults = function(){
return new Promise((resolve, reject) => {
students.forEach(fact => {
return engine.run(fact)
.then(() => resolve())
})
})
}
getResults().then(() => console.log(result));
我正在使用 json-rule-engine
。
https://www.npmjs.com/package/json-rules-engine
我有一个学生名单,上面有姓名和他们的百分比,我还有 business rule
百分比应该大于或等于 70
。 所以我想打印所有百分比超过 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!"
}
}
});
json-rules-engine 模块采用不同格式的数据。在你的 Repl.it 中你没有定义任何事实。
事实应该是:
let facts = [
{
name:"naveen",
percentage:70
},
[...]
此外,模块本身似乎不处理事实数组。您必须对其进行调整才能实现这一目标。这可以通过以下方式完成:
facts.forEach((fact) => {
engine
.run(fact)
[...]
终于在almanac
里面找到了学生数据。您可以通过以下方式获取这些值:results.almanac.factMap.get('[name|percentage|age|school|etc]').value
这是更新后的 Repl.it:https://repl.it/@adelriosantiago/json-rules-example
我可能提交了一个完全不相关的答案,但这里是。由于 students
对象是一个数组,您可以循环遍历它,然后使用 if else 语句。
for (let i = 0; i < students.length; i++) {
if (students[i].percentage >= 70) {
console.log(students[i].name);
}
}
抱歉,如果这不正确!
这是一个工作示例。
计数 success
和 failed
个案例
const { Engine } = require("json-rules-engine");
let engine = new Engine();
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
}]
},
event: { type: 'procedure_result'}
})
let result = {success_count : 0 , failed_count : 0}
engine.on('success', () => result.success_count++)
.on('failure', () => result.failed_count++)
const getResults = function(){
return new Promise((resolve, reject) => {
students.forEach(fact => {
return engine.run(fact)
.then(() => resolve())
})
})
}
getResults().then(() => console.log(result));