在数组对象中添加或删除指定的一项

    技术2022-07-11  103

    场景:this.userMenus = [{id:1},{id:2},{id:3}];row.id = 2 要求:在this.userMenus数组中找row.id,没有添加,有则删除符合条件这一项

    const result = this.userMenus.some(item => { if (item.id === row.id) { return true } }) if (result) { for (const item in this.userMenus) { if (this.userMenus[item].id === row.id) { this.userMenus.splice(item, 1) } } } else { this.userMenus.push({ id: row.id }) }
    Processed: 0.008, SQL: 9