4 回答

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)

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);

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);

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)
添加回答
舉報(bào)