4 回答
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)
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);
TA贡献1966条经验 获得超4个赞
如果我理解正确,您需要以下内容
const ids = array.filter(obj => obj.dependsOn && obj.dependsOn.includes('INS-SH-V')).map(cur => cur.id);
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)
添加回答
举报