数组函数有些没有按预期工作?我的代码有什么问题?
array function some not working as expected? What is wrong in my code?
const priceType = ['monthly', 'annual'];
var discounts =[
{product: "A", monthlyDiscount : 15.0, monthlyMaxDiscount: 30.0, annualDiscount : 25.0, annualMaxDiscount : 20.0},
{product: "B", monthlyDiscount : 5.0, monthlyMaxDiscount: 20.0, annualDiscount : 40.0, annualMaxDiscount : 30.0}
]
var exceedsDiscount;
如果我的任何折扣对象超过最大折扣,我需要将布尔值设置为 true 并停止检查其他数组项。
我使用了 for 循环,这按预期工作:
exceedsDiscount = false;
angular.forEach(priceType, function(type){
angular.forEach(discounts, function(discount){
if(discount[`${type}Discount`] > discount[`${type}MaxDiscount`]){
exceedsDiscount = true;
return;
}
});
如果我尝试使用 every 和 some 箭头函数,结果不相同。它随机给出真或假。知道如何在不使用两个 for 循环的情况下使用吗?
exceedsDiscount = false;
exceedsDiscount = priceType.every(type => discounts.some(discount =>{return discount[`${type}Discount`] > discount[`${type}MaxDiscount`];}));
如评论中 trincot 所述,如果使用 .some(... .some(...))
而不是 .every(.... .some(...))
- 将获得所需的结果。
'some .. some'
我们指的是这样一种情况,即存在“某些”类型的“某些”折扣大于 maxDiscount。
下面的片段:
const priceType = ['monthly', 'annual'];
var discounts = [{
product: "A",
monthlyDiscount: 15.0,
monthlyMaxDiscount: 30.0,
annualDiscount: 25.0,
annualMaxDiscount: 20.0
},
{
product: "B",
monthlyDiscount: 5.0,
monthlyMaxDiscount: 20.0,
annualDiscount: 40.0,
annualMaxDiscount: 30.0
}
];
const everySome = priceType.every(type => discounts.some(discount =>{return discount[`${type}Discount`] > discount[`${type}MaxDiscount`];}));;
const someSome = priceType.some(type => discounts.some(discount =>{return discount[`${type}Discount`] > discount[`${type}MaxDiscount`];}));
console.log(
'\n.every(.. .some( returns: ', JSON.stringify(everySome),
'\n.some(.. .some( returns: ', JSON.stringify(someSome)
);
const priceType = ['monthly', 'annual'];
var discounts =[
{product: "A", monthlyDiscount : 15.0, monthlyMaxDiscount: 30.0, annualDiscount : 25.0, annualMaxDiscount : 20.0},
{product: "B", monthlyDiscount : 5.0, monthlyMaxDiscount: 20.0, annualDiscount : 40.0, annualMaxDiscount : 30.0}
]
var exceedsDiscount;
如果我的任何折扣对象超过最大折扣,我需要将布尔值设置为 true 并停止检查其他数组项。
我使用了 for 循环,这按预期工作:
exceedsDiscount = false;
angular.forEach(priceType, function(type){
angular.forEach(discounts, function(discount){
if(discount[`${type}Discount`] > discount[`${type}MaxDiscount`]){
exceedsDiscount = true;
return;
}
});
如果我尝试使用 every 和 some 箭头函数,结果不相同。它随机给出真或假。知道如何在不使用两个 for 循环的情况下使用吗?
exceedsDiscount = false;
exceedsDiscount = priceType.every(type => discounts.some(discount =>{return discount[`${type}Discount`] > discount[`${type}MaxDiscount`];}));
如评论中 trincot 所述,如果使用 .some(... .some(...))
而不是 .every(.... .some(...))
- 将获得所需的结果。
'some .. some'
我们指的是这样一种情况,即存在“某些”类型的“某些”折扣大于 maxDiscount。
下面的片段:
const priceType = ['monthly', 'annual'];
var discounts = [{
product: "A",
monthlyDiscount: 15.0,
monthlyMaxDiscount: 30.0,
annualDiscount: 25.0,
annualMaxDiscount: 20.0
},
{
product: "B",
monthlyDiscount: 5.0,
monthlyMaxDiscount: 20.0,
annualDiscount: 40.0,
annualMaxDiscount: 30.0
}
];
const everySome = priceType.every(type => discounts.some(discount =>{return discount[`${type}Discount`] > discount[`${type}MaxDiscount`];}));;
const someSome = priceType.some(type => discounts.some(discount =>{return discount[`${type}Discount`] > discount[`${type}MaxDiscount`];}));
console.log(
'\n.every(.. .some( returns: ', JSON.stringify(everySome),
'\n.some(.. .some( returns: ', JSON.stringify(someSome)
);