Это может быть немного сложно, так как я не видел, чтобы кто-нибудь пытался сделать это, кроме стандартных операторов.
У меня есть таблица с примерно 50 тысячами строк объектов, каждая строка должна выполнить с ними набор выражений «до 30» и вернуть true или false. Я успешно сделал это с массовыми арендаторами, но все становится очень быстро.
У кого-то есть лучший подход? Минимальный код ниже - проблема заключается в функции проверки.
const objects = [
{
'First Name': 'Chris',
'Age': 18,
'Major': 'Mathematics',
'College Department': 'Mathematics'
},
{
'First Name': 'null',
'Age': 21,
'Major': 'Mathematics',
'College Department': 'Science'
}
]
const validate = (object, rule) => {
// logic to convert rule to logical expression
}
const results = objects.map(object => {
var flags = []
flags.push(validate(object, '[Fist Name] is null'))
flags.push(validate(object, '[Age] < [Required Age]'))
flags.push(validate(object, '[Major] === [College Department] and [Age] > [Required Age]'))
// Validate is supposed to return
// {Rule: '[Fist Name] is null', Flag: false/true, ...Rest of original object and key pairs}
// ... Return array of flags
return flags;
})
// Result should look like this
// lets say required age is 18
[
[
{Rule: '[Fist Name] is null', Flag: false, 'First Name': 'Chris', 'Age': 18, 'Major': 'Mathematics', 'College Department': 'Mathematics'},
{Rule: '[Age] < [Required Age]', Flag: false, 'First Name': 'Chris', 'Age': 18, 'Major': 'Mathematics', 'College Department': 'Mathematics'},
{Rule: '[Major] === [College Department] and [Age] > [Required Age]', Flag: true, 'First Name': 'Chris', 'Age': 18, 'Major': 'Mathematics', 'College Department': 'Mathematics'}
],
[
{Rule: '[Fist Name] is null', Flag: true, 'First Name': 'null', 'Age': 21, 'Major': 'Mathematics', 'College Department': 'Science'},
{Rule: '[Age] < [Required Age]', Flag: false, 'First Name': 'null', 'Age': 21, 'Major': 'Mathematics', 'College Department': 'Science'},
{Rule: '[Major] === [College Department] and [Age] > [Required Age]', Flag: false, 'First Name': 'null', 'Age': 21, 'Major': 'Mathematics', 'College Department': 'Science'}
]
]
// I know how to concat the arrays into one,
// so either the above output works or the one below works
[
{Rule: '[Fist Name] is null', Flag: false, 'First Name': 'Chris', 'Age': 18, 'Major': 'Mathematics', 'College Department': 'Mathematics'},
{Rule: '[Age] < [Required Age]', Flag: false, 'First Name': 'Chris', 'Age': 18, 'Major': 'Mathematics', 'College Department': 'Mathematics'},
{Rule: '[Major] === [College Department] and [Age] > [Required Age]', Flag: true, 'First Name': 'Chris', 'Age': 18, 'Major': 'Mathematics', 'College Department': 'Mathematics'},
{Rule: '[Fist Name] is null', Flag: true, 'First Name': 'null', 'Age': 21, 'Major': 'Mathematics', 'College Department': 'Science'},
{Rule: '[Age] < [Required Age]', Flag: false, 'First Name': 'null', 'Age': 21, 'Major': 'Mathematics', 'College Department': 'Science'},
{Rule: '[Major] === [College Department] and [Age] > [Required Age]', Flag: false, 'First Name': 'null', 'Age': 21, 'Major': 'Mathematics', 'College Department': 'Science'}
]
Обновление Это то, как далеко я прошел, но могу сделать это только для>, <=,> или, по крайней мере, это все, что я до сих пор тестировал. Я добавлю больше комментариев подробно в ближайшее время.
const object = {Age: 10, Required: 18};
const rules = [
{R: '["Age"] < ["Required"]', O: ['<']},
{R: '["Age"] <= ["Required"]', O: ['<=']},
{R: '["Age"] > ["Required"]', O: ['>']},
]
// Prototype that will parse the string
// ... then return the indexes of char
// ... we will use this to insert object name before the char
String.prototype.toIndices = function (d) { return this.split("").reduce((a, e, i) => e === d ? a.concat(i) : a, []) };
String.prototype.splice = function(idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
Object.prototype.validateRule = function (r) {
const newString = r['R'].toIndices("[").map(s => {
return r['R'].splice(s, 0, 'object');
})
var exp = [];
for (let item = 0; item < newString.length; item++) {
for (let obj = 0; obj < newString[item].split(" ").length; obj++) {
if (newString[item].split(" ")[obj].includes("object"))
exp.push(newString[item].split(" ")[obj])
}
}
return [...exp].map((e, i) => i < exp.length - 1 ? [e, r['O'][0]] : [e]).reduce((a, b) => a.concat(b)).join(" ");
}
console.log({Rule: rules[0]['R'], Flag: eval(object.validateRule(rules[0]))});
// output
/*
{ Rule: '["Age"] < ["Required"]', Flag: true }
*/
console.log(rules.map(rule => { return {Rule: rule['R'], Flag: eval(object.validateRule(rule))} }));
// output
/*
[ { Rule: '["Age"] < ["Required"]', Flag: true },
{ Rule: '["Age"] <= ["Required"]', Flag: true },
{ Rule: '["Age"] > ["Required"]', Flag: false } ]
*/