简体   繁体   中英

Lodash get the removed objects from array of objects

I have an array of objects like:

let users = [
     { id: 1, name: 'John Doe'},
     { id: 2, name: 'Robert Doe'},
     { id: 3, name: 'Mary Doe'}
];

And these are the IDs that needs to be removed:

let ids = [1, 3];

let el = _.remove( users, object => ids.includes(object.id) );

Now what I wanted is to get those users data removed. The output that I desire is:

[
     { id: 1, name: 'John Doe'}, 
     { id: 3, name: 'Mary Doe'}
] 

How can I achieve this? Thank you!

_.remove() :

Returns

(Array): Returns the new array of removed elements.

 let users = [ { id: 1, name: 'John Doe'}, { id: 2, name: 'Robert Doe'}, { id: 3, name: 'Mary Doe'} ]; let ids = [1, 3]; let removed = _.remove(users, (user) => ids.includes(user.id)); console.log('Original users array, without ids [ 1, 3 ]', users); console.log('Objects with ids [ 1, 3 ]', removed);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.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