0

I have this array:

var myArray = [{state: {name: 'object #1'}},
               {state: {name: 'object #2'}},
               {state: {name: 'object #3'}}];

I'm trying to remove with lodash the object where state.name is 'object #1'. Currently I'm doing it like this:

_.remove(myArray, {
        name: 'object #1'
    });

But it's not working. Initially I tried it like this, which for me is the more obvious way of doing it:

_.remove(myArray, {
        state.name: 'object #1'
    });

But it didn't worked either and my JS validator complained about the dot notation in there.

How do you do this in lodash?

5
  • It's because in JS: {} !== {}! Commented Mar 13, 2017 at 22:29
  • I've provided you an answer containing pure js solution. Even two solutions. Commented Mar 13, 2017 at 22:44
  • @arielcr Is requirement to remove an element from an existing array as described at original Question "Remove object from array with a nested property", or create a new array where specific elements are not included at new array? Commented Mar 13, 2017 at 22:54
  • 1
    @guest271314 the idea is to remove an element from the array Commented Mar 14, 2017 at 13:34
  • 1
    @arielcr You can use Array.prototype.splice() to remove element from an array. Commented Mar 14, 2017 at 20:05

6 Answers 6

4

_.remove takes a predicate function. You can do it this way:

_.remove(myArray, function(arg) {
    return arg.name == 'object #1';
});
Sign up to request clarification or add additional context in comments.

2 Comments

The predicate function must return something! In your case the returned value for all the items will be undefined which means the array will be empty afterwards as all its items have been removed!
Thanks @ibrahimmahrir!
3

In pure js, using Array#filter.

var myArray = [{state: {name: 'object #1'}},
               {state: {name: 'object #2'}},
               {state: {name: 'object #3'}}];

    myArray = myArray.filter(v => v.state.name != 'object #1');
    console.log(myArray);

or using Array#forEach.

var myArray = [{state: {name: 'object #1'}},
               {state: {name: 'object #2'}},
               {state: {name: 'object #3'}}],
    newArr = [];
    myArray.forEach(v => v.state.name !== 'object #1' ? newArr.push(v) : null);
    console.log(newArr);

7 Comments

Do approaches at javascript at Answer remove element from existing array, or create new array where an element of existing array is not included?
@guest271314 Yeah, was thinking about it, but I'm not sure that it's a good idea to modify the original obj :C
That is requirement at text of OP "Remove object from array with a nested property"?
@guest271314 Well, it's not precised exactly. You can remove an object from array without mutating it or with.
The text of original Question is precise from perspective here; remove specific y from x is different from create new x without specific y.
|
2

You can use lodash reject with a matchesProperty shorthand.

var result = _.reject(myArray, ['state.name', 'object #1']);

var myArray = [{
    state: {
      name: 'object #1'
    }
  },
  {
    state: {
      name: 'object #2'
    }
  },
  {
    state: {
      name: 'object #3'
    }
  }
];

var result = _.reject(myArray, ['state.name', 'object #1']);

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

2 Comments

@AdamBoduch unfortunately remove mutates the array, while reject doesn't.
Completely agree with you. I'd use reject() myself for immutability. Thought I'd mention it since remove() was the function in question.
1

You can use the callback of _.remove like this:

_.remove(myArray, function(o) {
    return o.name === 'object #1';
});

or shortly using arrow functions like this:

_.remove(myArray, o => o.name === 'object #1');

Comments

1

Why use lodash, when you can do it in plan JS?

myArray = myArray.filter(function(element) {
  return element.state.name !== 'object #1';
})

Comments

1

If requirement is to remove an element from an existing array, you can use Array.prototype.splice(), Array.prototype.findIndex()

var myArray = [{state: {name: 'object #1'}},
               {state: {name: 'object #2'}},
               {state: {name: 'object #3'}}];

myArray.splice(myArray.findIndex(({state:{name}}) => name === "object #1"), 1);

console.log(myArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.