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?

KooiInc
  • 119,216
  • 31
  • 141
  • 177
Boky
  • 11,554
  • 28
  • 93
  • 163
  • 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 – CertainPerformance Sep 02 '19 at 07:10
  • use **delete** javascript's itself. [Help](https://www.w3schools.com/howto/howto_js_remove_property_object.asp) – Nooruddin Lakhani Sep 02 '19 at 07: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) – briosheje Sep 02 '19 at 07:11
  • Is the property name and the object's `id` value always the same? If yes, you do not need a loop at all. – str Sep 02 '19 at 07:15
  • please add the missing function `forOwn`. – Nina Scholz Sep 02 '19 at 07:21
  • 1
    @NinaScholz https://lodash.com/docs/4.17.15#forOwn – str Sep 02 '19 at 07:21
  • 1
    just `delete obj['2']`... – georg Sep 02 '19 at 07:24

4 Answers4

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"},
}
AzamAbbasi
  • 91
  • 1
  • 9
  • 2
    This works but why add an external library if Javascript already has implemented it? – Patrick Lüthi Sep 02 '19 at 07:28
  • 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. – AzamAbbasi Sep 02 '19 at 07:36
  • 1
    I already use `lodash` in my project, thus your solution is fine and I think it's a little bit cleaner than my way. – Boky Sep 02 '19 at 07:51
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

Alex Szabo
  • 3,274
  • 2
  • 18
  • 30
0

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)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 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. – Chris Rollins Sep 02 '19 at 07:18
  • 1
    @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 – Code Maniac Sep 02 '19 at 07:20
-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.

Patrick Lüthi
  • 324
  • 2
  • 12
  • Please learn [the difference between JSON and the Object Literal Notation](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). Neither the question nor your answer is related to JSON. – str Sep 02 '19 at 07:16
  • 1
    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! – Patrick Lüthi Sep 02 '19 at 07:18
  • It does not seem unnecessary to me. `var json = { ... };` is semantically misleading as it is an object literal and not JSON. – str Sep 02 '19 at 07:44
  • As I just said. It is not the same but in some cases they act similar. In the case delete they are very similar. – Patrick Lüthi Sep 02 '19 at 08:31
  • "*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. – str Sep 02 '19 at 09:30
  • If JSON is just a string it is not a JSON. Obviously it is yet after calling the parse function. The delete operator is applicable to JSON ! – Patrick Lüthi Sep 02 '19 at 13:55
  • You are misunderstanding what JSON is. [JSON](http://json.org/) is a data serialization format. It is *always* a string. If it is not a string, it is not JSON. While the object notation in JavaScript is similar and it has obviously influenced the name of JSON, it is something else entirely. But that is all described in the link I posted in my first comment. Too bad you did not have a look at it. – str Sep 02 '19 at 13:59