gpt4 book ai didi

javascript - 如何在angularjs中使用_.map使用三元运算符

转载 作者:行者123 更新时间:2023-11-30 23:59:44 25 4
gpt4 key购买 nike

我需要根据某些条件返回 true 或 false,我没有得到更清晰的代码来执行此操作:

我需要根据此条件隐藏或显示 UI 上的某些元素,这些元素应返回 true 或 false:

scope.isDataEnabled =function(options){ //Here if supppose a comes,
// then i need to send true, otherwise false
if(!$rootScope.currentProduct.id ==
ProductConstants.BB_PRODUCT_ID){
return true;
}
else{
_.map( $rootScope.currentUser.permissions, permissionObj =>{
// permissions is an Array
return (permissionObj.module == "DATA" &&
permissionObj.values == "b" && options=="a") //need to send true
//if this condition satisfies
});
}
return false; //after returning true,
//it comes to this line and sets to false
}

请帮助我指导我应该使用什么来实现它。我正在考虑使用三元运算符,但不知道如何在 map 中使用它。

最佳答案

将代码简化为可操作的最小测试场景(并且仅使用普通的旧 ES20xx)可能会有所帮助。像这样的东西:

let $rootScope = {
currentProduct: {
id: `x`
},
currentUser: {
permissions: [{
module: `BB_DATASET`,
values: `b`
}, {
module: `some`,
values: `a`
}]
}
};
const ProductConstants = {
BB_PRODUCT_ID: `y`
}

console.log(testMe(`a`)); // true (currentProduct.id not BB_PRODUCT_ID)

ProductConstants.BB_PRODUCT_ID = `x`;
console.log(testMe(`a`)); // true (module && values && options)

$rootScope.currentUser.permissions[0].module = `AA_DATASET`;
console.log(testMe(`a`)); // false (module not BB_DATASET)

$rootScope.currentUser.permissions[0].module = `BB_DATASET`;
console.log(testMe(`b`)); // false (options not `a`)

$rootScope.currentUser.permissions[0].values = `c`;
console.log(testMe(`a`)); // false (values not `b`)

function testMe(options) {
if ($rootScope.currentProduct.id !== ProductConstants.BB_PRODUCT_ID) {
return true;
}
const found = $rootScope.currentUser.permissions.find( v =>
v.module === `BB_DATASET` &&
v.values === `b` &&
options == `a`);
return found ? true : false;

}
.as-console-wrapper {
top: 0;
max-height: 100% !important;
}

关于javascript - 如何在angularjs中使用_.map使用三元运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60816448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com