JavaScript 获取基于条件的逗号分隔值

问题描述 投票:0回答:0

嗨,我在过滤掉对象数组中的值时遇到问题。所以基本上我有一个对象数组

const arr = [
    {
        "id": null,
        "name": null,
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": null,
        "name": null,
        "role": "System Of Record (SOR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": null,
        "name": null,
        "role": "Authorized Redistributor (AR)",
        "checked": true,
        "checkBoxPatched": true
    }
]

arr.map(res => res.checked ? res.role : "").join(', ')
// ", System Of Record (SOR), Authorized Redistributor (AR)"

如果

role
为真,我需要逗号分隔的
checked
值。所以从上面的对象来看,O/P应该是
System Of Record (SOR), Authorized Redistributor (AR)
.

javascript arrays typescript object ecmascript-6
© www.soinside.com 2019 - 2024. All rights reserved.