Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete key-value pair from an object? [duplicate]

I have a variable data that contains some key-value pairs like this:

var data = {
    "1": 127,
    "2": 236,
    "3": 348
}

router.delete('/values/:id', function(req, res, next){
    var id = req.params.id;

})

How can I delete the key-value pair that has a key equal to the id variable?

like image 450
Bobimaru Avatar asked Dec 13 '17 14:12

Bobimaru


People also ask

How do you remove a particular key value pair from an array of objects?

Using delete operator. When only a single key is to be removed we can directly use the delete operator specifying the key in an object. Syntax: delete(object_name.

How do you remove duplicates in an object?

To remove the duplicates from an array of objects:Create an empty array that will store the unique object IDs. Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.


1 Answers

delete data[req.params.id] or delete data[id] should work.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

like image 94
Francisco Mateo Avatar answered Oct 21 '22 13:10

Francisco Mateo