来自 Array[object..] 的逻辑运算符
Logical operators from Array[object..]
我目前正在使用像这样的代码的布尔和逻辑运算符
仅供参考,我想转换数组中的 object.Value 和 object.Logic 以执行逻辑运算符。示例:object1.logic object.operators object2.logic 为真或假
const input: Array<any> = [
{
type: "basic",
value: true,
logic: "or"
},
{
type: "basic",
value: false,
logic: "and"
},
{
type: "basic",
value: true,
logic: "and"
},
我希望得到与
相同的结果
true || false && true
有人知道这个问题吗?
您只需要 运行 reduce
运算符和 return 一个新的运算符对象,该对象将具有先前运算符和值或当前计算的逻辑。
let input = [{
type: "basic",
value: true,
logic: "or"
},
{
type: "basic",
value: false,
logic: "and"
},
{
type: "basic",
value: true,
logic: "and"
}
]
// if type does not matter
let result = input.reduce((op1, op2) => ({
logic: op2.logic,
value: op1.logic === 'or' ? op1.value || op2.value : op1.value && op2.value
}));
// if you have more than ||- && operations you can create function with switch case and call it here.
console.log(result);
如果您想控制操作顺序:
let input = [{
type: "basic",
value: true,
logic: "or"
},
{
type: "basic",
value: false,
logic: "and"
},
{
type: "basic",
value: true,
logic: "and"
}
]
// logic: and - or
// operation: (boolean, boolean) => boolean;
let processOperation = (logic, operation, arr, op) => {
// if emplty return first
if (!arr.length) return [op];
let prevOp = arr[arr.length - 1];
if (prevOp.logic === logic) {
// remove prevous and replace with operation result
arr.pop()
arr.push({
logic: op.logic,
value: operation(prevOp.value, op.value)
});
} else {
arr.push(op); // if operation logic not matched push it in for future processing
}
return arr;
}
let result = input
.reduce(processOperation.bind(this, 'and', (a, b) => a && b), [])
.reduce(processOperation.bind(this, 'or', (a, b) => a || b), []);
// here you are deciding the order by the squence in the chiain
// result[0].value will be the solution
console.log('answer', result);
我目前正在使用像这样的代码的布尔和逻辑运算符
仅供参考,我想转换数组中的 object.Value 和 object.Logic 以执行逻辑运算符。示例:object1.logic object.operators object2.logic 为真或假
const input: Array<any> = [
{
type: "basic",
value: true,
logic: "or"
},
{
type: "basic",
value: false,
logic: "and"
},
{
type: "basic",
value: true,
logic: "and"
},
我希望得到与
相同的结果 true || false && true
有人知道这个问题吗?
您只需要 运行 reduce
运算符和 return 一个新的运算符对象,该对象将具有先前运算符和值或当前计算的逻辑。
let input = [{
type: "basic",
value: true,
logic: "or"
},
{
type: "basic",
value: false,
logic: "and"
},
{
type: "basic",
value: true,
logic: "and"
}
]
// if type does not matter
let result = input.reduce((op1, op2) => ({
logic: op2.logic,
value: op1.logic === 'or' ? op1.value || op2.value : op1.value && op2.value
}));
// if you have more than ||- && operations you can create function with switch case and call it here.
console.log(result);
如果您想控制操作顺序:
let input = [{
type: "basic",
value: true,
logic: "or"
},
{
type: "basic",
value: false,
logic: "and"
},
{
type: "basic",
value: true,
logic: "and"
}
]
// logic: and - or
// operation: (boolean, boolean) => boolean;
let processOperation = (logic, operation, arr, op) => {
// if emplty return first
if (!arr.length) return [op];
let prevOp = arr[arr.length - 1];
if (prevOp.logic === logic) {
// remove prevous and replace with operation result
arr.pop()
arr.push({
logic: op.logic,
value: operation(prevOp.value, op.value)
});
} else {
arr.push(op); // if operation logic not matched push it in for future processing
}
return arr;
}
let result = input
.reduce(processOperation.bind(this, 'and', (a, b) => a && b), [])
.reduce(processOperation.bind(this, 'or', (a, b) => a || b), []);
// here you are deciding the order by the squence in the chiain
// result[0].value will be the solution
console.log('answer', result);