0

I have an object as follows:

let object = {
   1: {id: 1, name: "One"},
   2: {id: 2, name: "Two"},
   3: {id: 3, name: "Three"}
}

And I want to remove one of them, for example the object with id 2

Now I do it using lodash as follows:

forOwn(object, function(value, key) {
     value.id === 2 && delete object[key]
});

That does the trick but is it the right way?

7
  • Sure, that works, though I'd prefer if rather than && - save the && for if you need the resulting expression, or when golfing. You can easily remove the dependency on lodash if you want as well Commented Sep 2, 2019 at 7:10
  • use delete javascript's itself. Help Commented Sep 2, 2019 at 7:11
  • I would add a null/undefined check (if (value && value.id === 2)) to avoid eventual null / undefined cases and, as said by @CertainPerformance, I would use an if instead of && (which does work, of course, but doesn't really give any benefit even in readability in your case specifically) Commented Sep 2, 2019 at 7:11
  • 1
    @NinaScholz lodash.com/docs/4.17.15#forOwn Commented Sep 2, 2019 at 7:21
  • 1
    just delete obj['2']... Commented Sep 2, 2019 at 7:24

4 Answers 4

1

You can use UnderscoreJs Library

let object = {
   1: {id: 1, name: "One"},
   2: {id: 2, name: "Two"},
   3: {id: 3, name: "Three"}
}

let newobject=_.remove(object,function(nv){
return nv.id===3;
});

the above code will delete the object having id=3 and return

{
  1: {id: 1, name: "One"},
  2: {id: 2, name: "Two"},
}
Sign up to request clarification or add additional context in comments.

3 Comments

This works but why add an external library if Javascript already has implemented it?
I appreciate but its just a way and depends what you want to do either using underscorejs or any other way it totally upto you.
I already use lodash in my project, thus your solution is fine and I think it's a little bit cleaner than my way.
1

I would use a simple for ... in instead of loadash

let object = {
   1: {id: 1, name: "One"},
   2: {id: 2, name: "Two"},
   3: {id: 3, name: "Three"}
}


let deleteById = (obj,idToRemove) => {
  for(let key in obj){
    let {id} = obj[key] | {}
    if(id === idToRemove){
      delete obj[key]
    }
  }
}

deleteById(object,2)
console.log(object)

2 Comments

this is less simple than the asker's code and harder to read and determine the intent at a glance, aside from the function name.
@ChrisRollins it's just a simple for in loop and if statement, i don't see how it' is less simple, apart from that it doesn't uses a library just for a normal inbuilt thing which can be done using native js
1

I think you're not getting the answers you're looking for, because maybe you've over simplified the use-case, so it's tempting to just say:

delete object[2]; // which modifies the original object

If your data is not that static, or if you want to do something more complex to remove certain elements, you could do something similar to this:

const relevantObjects = Object.entries(object) // converts each entry to [key, value]
  .filter(([k, v]) => v.id !== 2) // define the criteria to include/exclude items
  .reduce((acc, [k, v]) => {
    acc[k] = v;
    return acc; // this function can be improved, it converts the [[k, v]] back to {k: v, k: v, ...}
  }, {});

Edit:

It's perfectly fine to use a lib or whatever. Everything has its pros & cons, just do whatever works for you <3

Comments

-1

This is the right way. To do it in JSON you do the following:

var json = { ... };
var key = "foo";
delete json[key];

So if you swap out json to object you are all set.

7 Comments

Please learn the difference between JSON and the Object Literal Notation. Neither the question nor your answer is related to JSON.
As I mentioned you are doing that way in JSON and this is the correct way. I already know that JSON and Object is not the same! No need to comment such an unneccesary comment. I dont know why you all got that needs to do such things. Lets life in a simple way!
It does not seem unnecessary to me. var json = { ... }; is semantically misleading as it is an object literal and not JSON.
As I just said. It is not the same but in some cases they act similar. In the case delete they are very similar.
"In the case delete they are very similar." – No they are not. JSON is a string that you have to JSON.parse first before you can reliably remove a property. The delete operator is not applicable to JSON.
|

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.