为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕容3067478 2022-10-08 10:17:27
我正在关闭权限 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;
查看完整描述

4 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

您可以使用filter来过滤您的数组,然后使用 map 仅获取 id:


编辑:添加包括


const checkId = 'SOME ID'

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


查看完整回答
反对 回复 2022-10-08
?
当年话下

TA贡献1890条经验 获得超9个赞

您可以使用filter它,最后map使用数组,因为您只需要 id。作为示例,我添加了一个具有多个 id 的附加对象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);


查看完整回答
反对 回复 2022-10-08
?
慕标5832272

TA贡献1966条经验 获得超4个赞

如果我理解正确,您需要以下内容

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


查看完整回答
反对 回复 2022-10-08
?
冉冉说

TA贡献1877条经验 获得超1个赞

这是使用 for 循环的解决方案,


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)


查看完整回答
反对 回复 2022-10-08
  • 4 回答
  • 0 关注
  • 176 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信