将 pop() 与 JavaScript 关联数组一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21899818/
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-27 21:56:17  来源:igfitidea点击:

Use pop() with JavaScript Associative Arrays

javascriptarraysobjectassociative-array

提问by Il Profeta Profeta

How can I do something like the following in JS? I would like to imitate .pop()on an object rather than an array.

如何在 JS 中执行以下操作?我想模仿.pop()一个对象而不是一个数组。

var deck = { 
    'cardK' :'13',
    'cardQ' :'12',
    'cardAJ':'11'
};

var val = deck.pop();


console.log("Key" + val.key ); 
console.log("Value" + val.val ); 

It seems like it's not possible.

好像是不可能的。

回答by Ethan Brown

.popis only available on an array. In JavaScript, objects (which are essentially associative arrays) are not ordered like an array, so there is no .popmethod.

.pop仅在阵列上可用。在 JavaScript 中,对象(本质上是关联数组)不像数组那样有序,因此没有.pop方法。

You could use an array:

你可以使用一个数组:

var deck = [
    { key: 'cardK', val: 13 },
    { key: 'cardQ', val: 12 },
    { key: 'cardAJ', val: 11 },
];

var val = deck.pop();
console.log('key: ' + val.key);
console.log('aa: ' + val.val);

回答by Andrew Clark

As suggested by other answers, the best solution here might be to use an array of objects. However you could also create your own pop function that removes a key from an object, for example:

正如其他答案所建议的那样,这里的最佳解决方案可能是使用一组对象。但是,您也可以创建自己的 pop 函数,从对象中删除一个键,例如:

function pop(obj) {
    var key = Object.keys(obj).pop();
    var result = {key: key, val: obj[key]};
    delete obj[key];
    return result;
}

var val = pop(deck);

You could add a similar popfunction to Object.prototypeso that you could do deck.pop(), but I would strongly recommend against that type of design.

您可以添加类似的pop功能,Object.prototype以便您可以执行deck.pop(),但我强烈建议不要使用这种类型的设计。

回答by Felix Kling

You are right, it's not possible. See objects as mapsor hash tables, rather than "associative arrays". The properties don't have an order and thus a method such as .popwould not make sense (unless of course it would remove a random property, like Python's dictionaries).

你是对的,这是不可能的。将对象视为映射哈希表,而不是“关联数组”。属性没有顺序,因此诸如此类的方法.pop没有意义(当然,除非它会删除随机属性,例如 Python 的字典)。

If you want to to use .popand val.keyand val.val, you have to create an array of objects instead:

如果要使用.popand 和val.keyand val.val,则必须创建一个对象数组:

var deck = [
  {key: 'cardK', val: '13'},
  {key: 'cardQ', val: '12'},
  {key: 'cardAJ', val: '11'}
];

回答by Walter Roman

As I'm sure you know, .popis a prototypal Arraymethod, so you can't use it with Javascript objects.

正如我确定您知道的那样,它.pop是一种原型Array方法,因此您不能将它与 Javascript 对象一起使用。

Calling .popon an array will remove the lastelement from the array. However, there isn't a "last" key-value pair with objects, as their order is not ever guaranteed. Despite this, if you don't care about order, you could implement a .pop-like function for use with objects, though, again, it wouldn't remove and return the final key-value pair.

调用.pop数组将从数组中删除最后一个元素。但是,对象没有“最后一个”键值对,因为它们的顺序永远无法保证。尽管如此,如果您不关心顺序,您可以实现一个.pop用于对象的类似函数,但同样,它不会删除并返回最终的键值对。

Something like this should do the trick:

像这样的事情应该可以解决问题:

function pop(obj) {
  for (var key in obj) {
    var val = obj[key];
    delete obj[key];
    return {
        'key'   : key,
        'val'   : val,
    };
  };
};

Combined with your code:

结合您的代码:

var val = pop(deck);
console.log('key: ' + val.key);
console.log('aa: ' + val.val);

回答by Brian McGinity

When working with this structure, which can be thought of as an associative array, you need to use different techniques. Things like pop(), slice() and even .length will not work as they do with numeric keyed arrays.

当使用这个可以被认为是关联数组的结构时,您需要使用不同的技术。pop()、slice() 甚至 .length 之类的东西都不会像数字键控数组那样工作。

I use string keyed object arrays when searching for the key/value pair needs to happen fast.

当搜索键/值对需要快速发生时,我使用字符串键控对象数组。

Here's a jsPef I just created which shows the benefit of your array structure:

这是我刚刚创建的一个 jsPef,它显示了您的数组结构的好处:

http://jsperf.com/bmcgin-object-array-tests(keep in mind the performance goes way up as the array gets bigger)

http://jsperf.com/bmcgin-object-array-tests(请记住,随着数组变大,性能会上升)

Also keep in mind the value can be a number, a string, an array, a function, an object ect...

还要记住,值可以是数字、字符串、数组、函数、对象等...