Javascript 如何使用es6 every在嵌套数组上返回布尔值?
尝试使用Javascript 如何使用es6 every在嵌套数组上返回布尔值?,javascript,arrays,ecmascript-6,boolean,Javascript,Arrays,Ecmascript 6,Boolean,尝试使用everyjavascript方法如果任何indexID组合了firstFillCounter和refillCounter它应该返回retvaluetrue它的抛出undefined知道什么是更好的方法吗 梅因酒店 private arrayMatch(data) { const isCombined = this.validateOrder(data); if (isCombined) { return Promise.rej
every
javascript方法如果任何indexID
组合了firstFillCounter
和refillCounter
它应该返回retvalue
true它的抛出undefined
知道什么是更好的方法吗
梅因酒店
private arrayMatch(data) {
const isCombined = this.validateOrder(data);
if (isCombined) {
return Promise.reject(this.errorHandler(request, "First and Refill cant be combined"));
}
return isCombined;
}
private validateOrder(rxDetails) {
let isCombined: boolean = false;
isCombined = rxDetails.every( (o: any) => {
this.isCombinedFirstandRefill(o.rxInfos);
});
return isCombined;
}
private isCombinedFirstandRefill(rxInfos) {
let retVlaue: boolean = false;
let firstFillCounter: number = 0;
let refillCounter: number = 0;
for (const rx of rxInfos) {
if (rx.firstFillIndicator === "Y" && rx.acceptDigitalFirstFill === "Y") {
firstFillCounter++;
} else {
refillCounter++;
}
}
if (refillCounter > 1 && firstFillCounter > 1) {
retVlaue = true;
return retVlaue;
}
return retVlaue;
}
此返回true,并考虑无效请求。
const data = [{
indexID: 1,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "N",
rxNumber: "16021"
}
]
}]
这是有效的,它应该从isCombinedFirstandRefill返回false
const data1 = [{
indexID: 2,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "N",
rxNumber: "1512"
},
{
firstFillIndicator: "N",
acceptDigitalFirstFill: "N",
rxNumber: "16021"
}
]
}
];
传递给的函数需要返回一个布尔值。现在,正在传递的函数调用this.isCombinedFirstandRefill(o.rxInfos)代码>但不返回结果。请预先结束return
或删除花括号。@Hereticsmonkey您能给我一个您所指问题的示例吗?我所指问题的示例在问题的代码中。在validateOrder
函数中,存在以下代码:isCombined=rxDetails.every((o:any)=>{this.isCombinedFirstandRefill(o.rxInfos);})
函数(o:any)=>{this.isCombinedFirstandRefill(o.rxInfos);}
不向每个函数返回任何值(未定义的
)。这就是你的错误。您可以向其添加return
(o:any)=>{返回this.isCombinedFirstandRefill(o.rxInfos);}
或删除括号:(o:any)=>this.isCombinedFirstandRefill(o.rxInfos)
或将向我添加的@HereticMonkey返回一个值返回它抛出错误rxInfos不可编辑,但它是一个数组