我有一份菜单,里面有一份菜品清单。我希望能够从菜单中删除菜,我通过在菜单数组中拼接菜id的索引来实现这一点。但是,它不是删除值并缩短数组,而是用数组中的最后一个值替换该值。
在一个包含4个菜的菜单中,在移除其中3个菜之后,数组仍然包含4个值,它们都是相同的。
$scope.fjernRet = function(ret, menu) {
//console.log(ret._id)
var index = menu.retter.indexOf(ret._id);
if (index > -1) {
menu.retter.splice(index, 1)
}
menuService.update(menu);
socket.syncUpdates('menu', $scope.menuer);
}menu.retter可能看起来像
[{
_id: '56e827ba0ec7a8d02bf7747d',
name: 'test',
info: 'testinfo',
type: 'kød',
active: true
}, {
_id: '56e827ba0ec7a8d02bf77473',
name: 'test3',
info: 'testinfo3',
type: 'kød',
active: true
}, {
_id: '56e827ba0ec7a8d02bf77474',
name: 'test4',
info: 'testinfo4',
type: 'salat',
active: false
}];发布于 2016-03-18 00:41:17
替换此行:
var index = menu.retter.indexOf(ret._id);通过以下方式:
var index = menu.retter.map(function(x){
return x._id
}).indexOf(ret._id);Array.map()将返回一个只包含_id的映射数组,然后您的.indexOf()就可以工作了。
希望能有所帮助。
https://stackoverflow.com/questions/36066034
复制相似问题