我有对象数组 obj1,obj2,
如果对象名称不能有All键名则返回 true
如果对象名只能有一个All键名,其他对象返回false
如果对象的名称只能有一个All键名且没有其他对象返回 true
如果对象的名称有太多键名All返回 false
基于以上条件如何在 javascript 中执行。
function checkObj(ob){
var result = ob.filter(e=>e.name=="All");
if(result.length !== 1){
return false;
}
else{
return true;
}
}
var obj1=[
{id:1, name: "All", value:"all"},
{id:2, name: "Sun", value:"sun"},
{id:3, name: "Mon", value:"mon"},
]
var obj2=[
{id:4, name: "Thur", value:"thur"},
{id:8, name: "Mon", value:"mon"},
]
var obj3=[
{id:1, name: "Thur", value:"thur"},
{id:5, name: "All", value:"all"},
{id:2, name: "Mon", value:"mon"},
{id:6, name: "All", value:"all"}
]
var obj4=[
{id:1, name: "All", value:"all"}
]
var r1= this.checkObj(obj1);
var r2=this.checkObj(obj2);
var r3=this.checkObj(obj3);
var r4=this.checkObj(obj4);
Expected Output:
false
true
false
true
潇潇雨雨
九州编程
相关分类