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

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

如何正確過(guò)濾 JavaScript 中的對(duì)象?

如何正確過(guò)濾 JavaScript 中的對(duì)象?

慕容3067478 2022-10-08 10:17:27
我正在關(guān)閉權(quán)限 ID 并嘗試過(guò)濾具有這些權(quán)限的對(duì)象。這是 JSON 對(duì)象的簡(jiǎn)潔版本[  {    "id": "INS-SH-V",    "name": "View Sharing Functionality",    "description": "Access to view Email and Schedule windows",    "group": "Sharing",    "scopeable": false,    "featureId": "INS-BE",    "scopeExclusions": null,    "dependsOn": null  },  {    "id": "INS-SH-U",    "name": "Manage Sharing Functionality",    "description": "Access to send emails and schedule jobs",    "group": "Sharing",    "scopeable": false,    "featureId": "INS-BE",    "scopeExclusions": null,    "dependsOn": [      "INS-SH-V"    ]  } ]基本上我被給定INS-SH-V并且想要掃描每個(gè)dependsOn屬性中的所有項(xiàng)目,如果INS-SH-V有那么我想返回id那個(gè)對(duì)象的。有沒(méi)有一種簡(jiǎn)單的方法可以做到這一點(diǎn)?我一直在努力讓它在 for 循環(huán)中工作。謝謝!到目前為止,這是我的嘗試:let len = this.props.permissions.length;    for (let i = 0; i < len; i++){      if (this.props.permissions[i].dependsOn[0] !== null){        // return this.props.permissions[i].id // Should return INS-SH-U        if (permissionId === this.props.permissions[i].dependsOn[0]){          // return this.props.permissions[i].id          console.log(this.props.permissions[i].id);        }      }    }    return null;
查看完整描述

4 回答

?
慕桂英3389331

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

您可以使用filter來(lái)過(guò)濾您的數(shù)組,然后使用 map 僅獲取 id:


編輯:添加包括


const checkId = 'SOME ID'

const ids = yourData.filter(obj => obj['dependsOn'] !== null && obj['dependsOn'].includes(checkId)).map(obj => obj.id)


查看完整回答
反對(duì) 回復(fù) 2022-10-08
?
當(dāng)年話下

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

您可以使用filter它,最后map使用數(shù)組,因?yàn)槟恍枰?id。作為示例,我添加了一個(gè)具有多個(gè) id 的附加對(duì)象dependsOn。


const inputArr = [

  {

    "id": "INS-SH-V",

    "name": "View Sharing Functionality",

    "description": "Access to view Email and Schedule windows",

    "group": "Sharing",

    "scopeable": false,

    "featureId": "INS-BE",

    "scopeExclusions": null,

    "dependsOn": null

  },

  {

    "id": "INS-SH-U",

    "name": "Manage Sharing Functionality",

    "description": "Access to send emails and schedule jobs",

    "group": "Sharing",

    "scopeable": false,

    "featureId": "INS-BE",

    "scopeExclusions": null,

    "dependsOn": [

      "INS-SH-V"

    ]

  },

  {

    "id": "INS-SH-asdsd",

    "name": "View Sharing Functionality",

    "description": "Access to view Email and Schedule windows",

    "group": "Sharing",

    "scopeable": false,

    "featureId": "INS-BE",

    "scopeExclusions": null,

    "dependsOn": [

        'INS-SH-V',

        'INS-SH-A'

    ]

  },

 ];


const idToCheck = 'INS-SH-V';

const result = inputArr.filter(obj => (obj.dependsOn?.includes(idToCheck) || false)).map(({ id }) => id);

console.log(result);


查看完整回答
反對(duì) 回復(fù) 2022-10-08
?
慕標(biāo)5832272

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

如果我理解正確,您需要以下內(nèi)容

const ids = array.filter(obj => obj.dependsOn && obj.dependsOn.includes('INS-SH-V')).map(cur => cur.id);


查看完整回答
反對(duì) 回復(fù) 2022-10-08
?
冉冉說(shuō)

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

這是使用 for 循環(huán)的解決方案,


let data = [{

    "id": "INS-SH-V",

    "name": "View Sharing Functionality",

    "description": "Access to view Email and Schedule windows",

    "group": "Sharing",

    "scopeable": false,

    "featureId": "INS-BE",

    "scopeExclusions": null,

    "dependsOn": null

  },

  {

    "id": "INS-SH-U",

    "name": "Manage Sharing Functionality",

    "description": "Access to send emails and schedule jobs",

    "group": "Sharing",

    "scopeable": false,

    "featureId": "INS-BE",

    "scopeExclusions": null,

    "dependsOn": [

      "INS-SH-V"

    ]

  }

]


function filterByDependsOn(val) {

  let result = []



  for (let obj of data) {

    if (obj.dependsOn && obj.dependsOn.includes(val)) {

      result.push(obj.id)

    }

  }


  return result

}


console.log(filterByDependsOn('INS-SH-V'))

如果您想要單線解決方案,


let data = [{

    "id": "INS-SH-V",

    "name": "View Sharing Functionality",

    "description": "Access to view Email and Schedule windows",

    "group": "Sharing",

    "scopeable": false,

    "featureId": "INS-BE",

    "scopeExclusions": null,

    "dependsOn": null

  },

  {

    "id": "INS-SH-U",

    "name": "Manage Sharing Functionality",

    "description": "Access to send emails and schedule jobs",

    "group": "Sharing",

    "scopeable": false,

    "featureId": "INS-BE",

    "scopeExclusions": null,

    "dependsOn": [

      "INS-SH-V"

    ]

  }

]


const filteredData = data.filter(el => el.dependsOn && el.dependsOn.includes('INS-SH-V')).map(v => v.id)


console.log(filteredData)


查看完整回答
反對(duì) 回復(fù) 2022-10-08
  • 4 回答
  • 0 關(guān)注
  • 199 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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