Проблема при удалении объекта с помощью Lodash _remove - PullRequest
0 голосов
/ 24 октября 2018

У меня есть объект:

ids = [ "-LIof_e0hPtXKtkc4Uh9", "-LIjBcGO7m7VQ-B3pfYt" ]

Если я выполняю итерацию, используя .map функцию lodash

_.map(ids, (userID, key) => {
     console.log('Lopping userId',userID);
})

, это дает мне значение каждого идентификатора.

Теперь, когда я пытаюсь удалить его, используя _remove, он работает не так, как ожидалось.

_.remove(ids, (value, key, obj) => value == idToBeRemoved);

Тем не менее, в ids Object нет никакой разницы.

Ядействительно новый для angular4 и впервые использующий lodash.Я просто хотел удалить значение из ids объекта и получить оставшийся объект.

Печать консоли.

enter image description here Я использую 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)
        });
      }
  }

Ответы [ 3 ]

0 голосов
/ 24 октября 2018

Вы можете просто использовать lodash _. Pull

const _ = require("lodash");
const ids = [ "-LIof_e0hPtXKtkc4Uh9", "-LIjBcGO7m7VQ-B3pfYt" ]
const result = _.pull(ids, "-LIjBcGO7m7VQ-B3pfYt" )
console.log(filteredIds)
0 голосов
/ 01 апреля 2019

Сначала найдите индекс того, что вы удалили элемент, а затем извлеките из него _.pullAt (lodash)

    let getIndex= _.findIndex(this.images, function(o) { return o.name == img.name; });
    let removedImage = _.pullAt(this.images, getIndex) ;
0 голосов
/ 24 октября 2018

Вы хотите _filter вместо

const ids = [ "-LIof_e0hPtXKtkc4Uh9", "-LIjBcGO7m7VQ-B3pfYt" ]
const idToBeRemoved = "-LIof_e0hPtXKtkc4Uh9"
const filteredIds = _.filter(ids, function(id) { return id !== idToBeRemoved})
console.log(filteredIds)
...