如何正确过滤 JavaScript 中的对象?

我正在关闭权限 ID 并尝试过滤具有这些权限的对象。这是 JSON 对象的简洁版本


[

  {

    "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并且想要扫描每个dependsOn属性中的所有项目,如果INS-SH-V有那么我想返回id那个对象的。有没有一种简单的方法可以做到这一点?我一直在努力让它在 for 循环中工作。


谢谢!


到目前为止,这是我的尝试:


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;


慕容3067478
浏览 177回答 4
4回答

慕桂英3389331

您可以使用filter来过滤您的数组,然后使用 map 仅获取 id:编辑:添加包括const checkId = 'SOME ID'const ids = yourData.filter(obj => obj['dependsOn'] !== null && obj['dependsOn'].includes(checkId)).map(obj => obj.id)

当年话下

您可以使用filter它,最后map使用数组,因为您只需要 id。作为示例,我添加了一个具有多个 id 的附加对象dependsOn。const inputArr = [&nbsp; {&nbsp; &nbsp; "id": "INS-SH-V",&nbsp; &nbsp; "name": "View Sharing Functionality",&nbsp; &nbsp; "description": "Access to view Email and Schedule windows",&nbsp; &nbsp; "group": "Sharing",&nbsp; &nbsp; "scopeable": false,&nbsp; &nbsp; "featureId": "INS-BE",&nbsp; &nbsp; "scopeExclusions": null,&nbsp; &nbsp; "dependsOn": null&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "INS-SH-U",&nbsp; &nbsp; "name": "Manage Sharing Functionality",&nbsp; &nbsp; "description": "Access to send emails and schedule jobs",&nbsp; &nbsp; "group": "Sharing",&nbsp; &nbsp; "scopeable": false,&nbsp; &nbsp; "featureId": "INS-BE",&nbsp; &nbsp; "scopeExclusions": null,&nbsp; &nbsp; "dependsOn": [&nbsp; &nbsp; &nbsp; "INS-SH-V"&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "INS-SH-asdsd",&nbsp; &nbsp; "name": "View Sharing Functionality",&nbsp; &nbsp; "description": "Access to view Email and Schedule windows",&nbsp; &nbsp; "group": "Sharing",&nbsp; &nbsp; "scopeable": false,&nbsp; &nbsp; "featureId": "INS-BE",&nbsp; &nbsp; "scopeExclusions": null,&nbsp; &nbsp; "dependsOn": [&nbsp; &nbsp; &nbsp; &nbsp; 'INS-SH-V',&nbsp; &nbsp; &nbsp; &nbsp; 'INS-SH-A'&nbsp; &nbsp; ]&nbsp; },&nbsp;];const idToCheck = 'INS-SH-V';const result = inputArr.filter(obj => (obj.dependsOn?.includes(idToCheck) || false)).map(({ id }) => id);console.log(result);

慕标5832272

如果我理解正确,您需要以下内容const&nbsp;ids&nbsp;=&nbsp;array.filter(obj&nbsp;=>&nbsp;obj.dependsOn&nbsp;&&&nbsp;obj.dependsOn.includes('INS-SH-V')).map(cur&nbsp;=>&nbsp;cur.id);

冉冉说

这是使用 for 循环的解决方案,let data = [{&nbsp; &nbsp; "id": "INS-SH-V",&nbsp; &nbsp; "name": "View Sharing Functionality",&nbsp; &nbsp; "description": "Access to view Email and Schedule windows",&nbsp; &nbsp; "group": "Sharing",&nbsp; &nbsp; "scopeable": false,&nbsp; &nbsp; "featureId": "INS-BE",&nbsp; &nbsp; "scopeExclusions": null,&nbsp; &nbsp; "dependsOn": null&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "INS-SH-U",&nbsp; &nbsp; "name": "Manage Sharing Functionality",&nbsp; &nbsp; "description": "Access to send emails and schedule jobs",&nbsp; &nbsp; "group": "Sharing",&nbsp; &nbsp; "scopeable": false,&nbsp; &nbsp; "featureId": "INS-BE",&nbsp; &nbsp; "scopeExclusions": null,&nbsp; &nbsp; "dependsOn": [&nbsp; &nbsp; &nbsp; "INS-SH-V"&nbsp; &nbsp; ]&nbsp; }]function filterByDependsOn(val) {&nbsp; let result = []&nbsp; for (let obj of data) {&nbsp; &nbsp; if (obj.dependsOn && obj.dependsOn.includes(val)) {&nbsp; &nbsp; &nbsp; result.push(obj.id)&nbsp; &nbsp; }&nbsp; }&nbsp; return result}console.log(filterByDependsOn('INS-SH-V'))如果您想要单线解决方案,let data = [{&nbsp; &nbsp; "id": "INS-SH-V",&nbsp; &nbsp; "name": "View Sharing Functionality",&nbsp; &nbsp; "description": "Access to view Email and Schedule windows",&nbsp; &nbsp; "group": "Sharing",&nbsp; &nbsp; "scopeable": false,&nbsp; &nbsp; "featureId": "INS-BE",&nbsp; &nbsp; "scopeExclusions": null,&nbsp; &nbsp; "dependsOn": null&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "INS-SH-U",&nbsp; &nbsp; "name": "Manage Sharing Functionality",&nbsp; &nbsp; "description": "Access to send emails and schedule jobs",&nbsp; &nbsp; "group": "Sharing",&nbsp; &nbsp; "scopeable": false,&nbsp; &nbsp; "featureId": "INS-BE",&nbsp; &nbsp; "scopeExclusions": null,&nbsp; &nbsp; "dependsOn": [&nbsp; &nbsp; &nbsp; "INS-SH-V"&nbsp; &nbsp; ]&nbsp; }]const filteredData = data.filter(el => el.dependsOn && el.dependsOn.includes('INS-SH-V')).map(v => v.id)console.log(filteredData)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript