第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù)數(shù)組對象javascript中的條件檢查返回布爾值

如何根據(jù)數(shù)組對象javascript中的條件檢查返回布爾值

開心每一天1111 2023-05-25 17:22:37
我有對象數(shù)組 obj1,obj2,如果對象名稱不能有All鍵名則返回 true如果對象名只能有一個(gè)All鍵名,其他對象返回false如果對象的名稱只能有一個(gè)All鍵名且沒有其他對象返回 true如果對象的名稱有太多鍵名All返回 false基于以上條件如何在 javascript 中執(zhí)行。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:falsetruefalsetrue
查看完整描述

2 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以檢查數(shù)組是否包含任何對象name = "All"。


那么,return true如果


數(shù)組有name = "All"對象,數(shù)組的長度是1

或者數(shù)組沒有name = "All"

function checkObj(arr) {

  const hasAll = arr.some(o => o.name === "All")

  return (arr.length === 1 && hasAll) || !hasAll

}

這是一個(gè)片段:

function checkObj(arr) {

  const hasAll = arr.some(o => o.name === "All")

  return (arr.length === 1 && hasAll) || !hasAll

}


const obj1=[{id:1,name:"All",value:"all"},{id:2,name:"Sun",value:"sun"},{id:3,name:"Mon",value:"mon"},],

    obj2=[{id:4,name:"Thur",value:"thur"},{id:8,name:"Mon",value:"mon"},],

    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"}],

    obj4=[{id:1,name:"All",value:"all"}];


console.log( checkObj(obj1) )

console.log( checkObj(obj2) )

console.log( checkObj(obj3) )

console.log( checkObj(obj4) )


查看完整回答
反對 回復(fù) 2023-05-25
?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

你可以Array#every在計(jì)數(shù)超過一時(shí)短路。

function checkArray(array) {

? ? return array.every((count => ({ name }) => (count += name === "All") < 2)(0));

}


var array1 = [{ id: 1, name: "All", value: "all" }, {? id: 2, name: "Sun", value: "sun" }, { id: 3, name: "Mon", value: "mon" }],

? ? array2 = [{ id: 4, name: "Thur", value: "thur" }, { id: 8,? name: "Mon", value: "mon" }],

? ? array3 = [{ id: 1, name: "Thur", value: "thur" }, { id: 5, name: "All",? value: "all" }, { id: 2, name: "Mon",? value: "mon" }, { id: 6, name: "All", value: "all" }];


console.log(checkArray(array1), checkArray(array2), checkArray(array3));



查看完整回答
反對 回復(fù) 2023-05-25
  • 2 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)