简体   繁体   中英

Remove an object from an array using lodash

I want to remove an object from an array by the id how many times the user wants to

num = number of times that the user wants to remove.

I'm using the '_.find' to find if the id of the product matches with the object.

I've already tried use '_.remove' but this remove all with the same id,and i just want to remove how many times the user puts

My code

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

Object 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"
  }
]

You can use lodash _.filter() (or vanilla JS Arrayfilter() or _.reject() , and decrement a counter . As long as the value in the counter is greater than 0, and the ids matches, remove the element from the array.

Note: _.remove() mutates the original array, and return an array of the removed items, so in this case I prefer the filter/reject method that return a new array without the removed items.

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM