0

I want remove with Lodash, the object with uuid: xxx and not xxx2.

How I can make this with Lodash ?

[
    {
        "uuid": "xxx",
        "name": "test"
    },
    {
        "uuid": "xxx2",
        "name": "test 2"
    }
]

My actual code:

_.forEach(this.objs, (obj, index, collection) => {
   _.remove(obj, {uuid})
})
pirmax
  • 2,054
  • 8
  • 36
  • 69

2 Answers2

3

You can simple use _.remove method.

var objects = [
    {
        "uuid": "xxx",
        "name": "test"
    },
    {
        "uuid": "xxx2",
        "name": "test 2"
    }
];

_.remove(objects, o => o.uuid === 'xxx2');

console.log(objects);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19
0

You can use Array.prototype.filter() for that

let arr = [
    {
        "uuid": "xxx",
        "name": "test"
    },
    {
        "uuid": "xxx2",
        "name": "test 2"
    }
]


arr = arr.filter(e => e.uuid != 'xxx2');

console.log(arr);
marvel308
  • 10,288
  • 1
  • 21
  • 32