使用lodash从数组中删除对象

时间:2019-08-14 17:31:43

标签: javascript arrays object lodash

我想通过ID从数组中删除一个对象,用户希望这样做

num =用户要删除的次数。

我正在使用'_.find'查找产品ID是否与对象匹配。

我已经尝试使用'_.remove',但这会删除所有具有相同ID的东西,而我只想删除用户输入的次数

我的代码

_.times(num, () => {
    _.remove(this.businessItem.associatedProducts, function() {
           return _.find(this.businessItem.associatedProducts, {id: product.id});
         });
      });

对象businessItem.associatedProducts

[{
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  },
  {
    archived: false,
    archivedAt: null,
    categoriesFilter: {},
    category: {},
    categoryId: "7MdXH5wcrzN7SSQkDEUT"
  }
]

1 个答案:

答案 0 :(得分:0)

您可以使用lodash _.filter()(或香草JS Arrayfilter()_.reject(),并递减counter。只要计数器中的值大于0,并且ID匹配,则从数组中删除该元素。

注意:_.remove()改变了原始数组,并返回一个已删除项目的数组,因此在这种情况下,我更喜欢使用filter / reject方法,该方法返回一个不删除已删除项目的新数组。

const fn = (num, id, arr) => {
  let counter = num
  
  return _.reject(arr, o => counter-- > 0 && o.categoryId === id)
}

const arr = [{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"},{"archived":false,"archivedAt":null,"categoriesFilter":{},"category":{},"categoryId":"7MdXH5wcrzN7SSQkDEUT"}]

const result = fn(9, '7MdXH5wcrzN7SSQkDEUT', arr)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>