javascript - How to exclude a specific id from an array? -
i have array sports names, option check , uncheck them.
the items checked = false
being saved in db. default items in checked = true
, but, want exclude 1 of items in array , put checked = false
instead.
at first, array looks this
[ { "id": 26, "name": "live betting", "priority": 0 }, { "id": 8, "name": "nba", "priority": 1 }, { "id": 24, "name": "college basketball", "priority": 2 }, { "id": 42, "name": "women college basketball", "priority": 3 }, { "id": 9, "name": "nhl", "priority": 4 }, { "id": 6, "name": "mlb", "priority": 5 } ]
and function working on here
_this.getsportchecked(customer).then(function(sportchecked) { var sportids = _.pluck(sports, 'id'), intersectedsports = _.intersection(sportids, sportchecked.sport); if (sports.length) { //here need exclude id 9 sports = _.map(sports, function(sport) { sport.checked = !_.includes(intersectedsports, sport.id); return sport; }); } });
which returns objects
{ "id": 26, "name": "live betting", "priority": 0, "checked": true } { "id": 8, "name": "nba", "priority": 1, "checked": true } { "id": 24, "name": "college basketball", "priority": 2, "checked": true } { "id": 42, "name": "women college basketball", "priority": 3, "checked": true } { "id": 9, "name": "nhl", "priority": 4, "checked": true } { "id": 6, "name": "mlb", "priority": 5, "checked": true }
so can see items checked = true
default, want set id = 9
checked = false
default until user switches true in case wants.
what suggestions here? read note in code please, there part need exclude it.
well, can find sport id , uncheck manually.
if (sports.length) { //here need exclude id 9 sports = _.map(sports, function(sport) { sport.checked = !_.includes(intersectedsports, sport.id); // special case id 9 if (sport.id === 9) { sport.checked = false; } return sport; }
Comments
Post a Comment