【发布时间】:2021-11-18 08:20:32
【问题描述】:
无论如何,如果不使用影响其性能的 eval() 或 new function() 来动态传递 if 语句的条件及其运算符。
<script>
var Students = [{
"name": "Raj",
"Age":"15",
"RollNumber": "123",
"Marks": "99",
}, {
"name": "Aman",
"Age":"14",
"RollNumber": "223",
"Marks": "69",
},
{
"name": "Vivek",
"Age":"13",
"RollNumber": "253",
"Marks": "89",
}
];
*Number of condition are dynamic. can even contain 3 or 4 condition at a time*
var condition = [{"attribute":"el.Age","operator":">=","value":14},
{"attribute":"el.Marks","operator":"<=","value":70}];
var newArray =Students.filter(function (el)
{
if( condition[0].attribute condition[0].operator condition[0].value && condition[1].attribute condition[1].operator condition[1].value ){
return true;
});
console.log(newArray);
</script>
【问题讨论】:
标签: javascript arrays object if-statement dynamic