简体   繁体   English

'关联数组'/对象的Javascript'First或Default'函数

[英]Javascript 'First or Default' function for 'associative arrays'/objects

Is there a better way to do this? 有一个更好的方法吗?

I'm storing values in what some would erroneously call an associated array: 我将值存储在一些错误地称为关联数组的值中:

The tokens object stores.... tokens and a count of documents using that token on a per-db level. tokens对象存储....令牌以及在每个db级别上使用该令牌的文档计数。

var tokens = {'db1' : { '654321': { 'docCount': 1 },
                        '321456': { 'docCount': 2 } },
              'db2' : { '999999': { 'docCount': 1 } } };

I can add/remove dbs and tokens and update the docCounts appropriately. 我可以添加/删除dbs和令牌并适当更新docCounts。 We can assume, due to code omitted for brevity, that if a db exists, a token also exists with a docCount of at least 1. 由于为简洁起见省略了代码,我们可以假设,如果存在db,则还存在一个docCount至少为1的令牌。

If a db exists and I need to retrieve ANY of its tokens, what is the best method? 如果存在db并且我需要检索其任何标记,那么最好的方法是什么? If the dbs held arrays, it would be as easy as tokens['db1'][0] ... but I'm not using arrays. 如果dbs持有数组,它就像tokens['db1'][0]一样容易......但我没有使用数组。

I have something like the following, "inspired" by LINQ (please don't blame LINQ): 我有类似下面这样的内容,受到LINQ的“启发”(请不要责怪LINQ):

// NOTE: default not implemented here
var firstOrDefault = function(obj) {
  var thing;
  for (var i in obj) {
    thing = i;
    break;
  }
  return thing;
};

which would be called as so (simplified for example): 这将被称为(例如简化):

var anyToken;
if (tokens['db1') { anyToken = firstOrDefault(tokens['db1']); }

Generally returning per the above example '654321' (as this is an object, not an array, order is not guaranteed, but either value is acceptable in my code). 通常按照上面的示例'654321'返回(因为这是一个对象,而不是数组,不保证顺序,但我的代码中的任何一个值都是可接受的)。

  • Is this a reasonable method to get any value? 这是获得任何价值的合理方法吗?
  • Is there a better method? 有更好的方法吗?
  • Should I just suck it up, shove everything into an array, and wrap the storage features that way? 我应该把它吸干,把所有东西都塞进一个数组中,并用这种方式包装存储功能吗?

UPDATE: I've removed the default reference, as an unfound item will a perfectly acceptable undefined response: 更新:我已经删除了default引用,因为一个未完成的项目将是一个完全可接受的undefined响应:

// NOTE: obj.hasOwnProperty not implemented for brevity
var firstOrAny = function(obj) {
  var thing;
  for (var i in obj) {
    thing = i;
    break;
  }
  return thing;
};

which would be called as so (simplified for example): 这将被称为(例如简化):

var anyToken;
if (tokens['db1') { anyToken = firstOrAny(tokens['db1']); }

Slightly shorter solution: 略短的解决方案:

var firstOrDefault = function(obj, d) { 
  for (var i in obj)
  {
    if (obj.hasOwnProperty(i))
    {
      return obj[i];
    }
  }
  return d; 
};

But yes, it is the fastest way to get any (usually first inserted) key from an object. 但是,是的,它是从对象获取任何(通常是第一次插入)键的最快方法。

I also added a hasOwnProperty check to prevent cases where the values are retrieved from the prototype chain. 我还添加了一个hasOwnProperty检查,以防止从原型链中检索值的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM