javascript 如何从对象中删除键值对?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47795968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:35:38  来源:igfitidea点击:

How to delete key-value pair from an object?

javascriptnode.js

提问by Bobimaru

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?

如何删除键等于 id 变量的键值对?

回答by Francisco Mateo

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

delete data[req.params.id]delete data[id]应该工作。

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

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