Javascript 使用Lodash删除对象时出现问题\u删除
我有一个目标:Javascript 使用Lodash删除对象时出现问题\u删除,javascript,angular,firebase,lodash,Javascript,Angular,Firebase,Lodash,我有一个目标: ids = [ "-LIof_e0hPtXKtkc4Uh9", "-LIjBcGO7m7VQ-B3pfYt" ] 如果我使用lodash _.map(ids, (userID, key) => { console.log('Lopping userId',userID); }) 它给了我每个id的值 现在,当我试图使用\u remove删除它时,它没有按预期工作 _.remove(ids, (value, key, obj) => value == id
ids = [ "-LIof_e0hPtXKtkc4Uh9", "-LIjBcGO7m7VQ-B3pfYt" ]
如果我使用lodash
_.map(ids, (userID, key) => {
console.log('Lopping userId',userID);
})
它给了我每个id的值
现在,当我试图使用\u remove
删除它时,它没有按预期工作
_.remove(ids, (value, key, obj) => value == idToBeRemoved);
在ids
对象中仍然没有区别
我对angular4真的很陌生,第一次使用lodash。
我只是想从ids
对象中删除一个值,然后获取剩余的对象
控制台的打印
我正在使用firebase并在从firebase检索数据后尝试删除数据:
deleteTransactWith(transactWithKey,transactWithUser) {
let currentUser = this._authService.getLoggedInUser();
console.log(transactWithUser.groups)
console.log(Object.keys(transactWithUser.groups).length)
console.log('key To remove',transactWithKey)
for (var key in transactWithUser.groups) {
if (transactWithUser.groups.hasOwnProperty(key)) {
let group = this.firebase.object(`/url/${currentUser.$key}/${key}`);
group.snapshotChanges().take(1).subscribe((groupData: any) => {
groupData = groupData.payload.toJSON();
//console.log('not removed',groupData.userIds)
console.log('key',transactWithKey)
_.remove(groupData.userIds, (value) => value == transactWithKey);
//_.pull(groupData.userIds, transactWithKey);
console.log('removed',groupData.userIds)
});
}
}
你想要什么
你可以简单地使用lodash
首先找到要删除项目的索引,然后通过u.pullAt(lodash)从中取出
你的
map
函数打算做什么?什么都没有。我只是想检查键和值“我有一个对象:”--不,你没有,你有一个数组。你有一个元素数组。我附上了控制台更新答案的图片。我在我的问题中添加了一个代码。请检查我从firebase获取数据的最后一个函数。你能提供你的答案吗?谢谢你的工作。你能解释一下为什么搬家不起作用,甚至在路上都没有。今晚晚些时候,我将介绍类似的方法。它们从源数组中删除元素并返回它们。remove使用函数进行比较;\ux。pull获取要比较的值列表。filter使用函数匹配项并返回除匹配项外的所有项。不会更改源集合。使用什么取决于是否要更改原始数组、是否需要匹配项,或者是否需要除匹配项以外的所有匹配项。
const ids = [ "-LIof_e0hPtXKtkc4Uh9", "-LIjBcGO7m7VQ-B3pfYt" ]
const idToBeRemoved = "-LIof_e0hPtXKtkc4Uh9"
const filteredIds = _.filter(ids, function(id) { return id !== idToBeRemoved})
console.log(filteredIds)
const _ = require("lodash");
const ids = [ "-LIof_e0hPtXKtkc4Uh9", "-LIjBcGO7m7VQ-B3pfYt" ]
const result = _.pull(ids, "-LIjBcGO7m7VQ-B3pfYt" )
console.log(filteredIds)
let getIndex= _.findIndex(this.images, function(o) { return o.name == img.name; });
let removedImage = _.pullAt(this.images, getIndex) ;