按键和返回值从哈希中删除

Remove from hash by key and return value

本文关键字:哈希中 删除 返回值      更新时间:2023-09-26

是否有某种方法可以通过键从哈希中移除并返回移除的值。即该代码:

var a = attributes['a']
delete attributes['a']

在单行中。Smth类似Ruby的delete:

a = attributes.delete(:a)

在一行中,是:

var a = attributes['a']; delete attributes['a'];

在一句简单的话中,没有

你可以想象一个函数为你做这件事,但由于你不能在ES5中传递属性,只能传递它的名称或值,你必须使用两个参数:

function deleteAndReturn(map, key) {
    var a = map[key];
    delete map[key];
    return a;
}