最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

properties - Javascript 'First or Default' function for 'associative arrays'objects - Stack Over

programmeradmin3浏览0评论

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.

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. 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.

If a db exists and I need to retrieve ANY of its tokens, what is the best method? If the dbs held arrays, it would be as easy as tokens['db1'][0]... but I'm not using arrays.

I have something like the following, "inspired" by LINQ (please don't blame 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).

  • 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:

// 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']); }

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.

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. 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.

If a db exists and I need to retrieve ANY of its tokens, what is the best method? If the dbs held arrays, it would be as easy as tokens['db1'][0]... but I'm not using arrays.

I have something like the following, "inspired" by LINQ (please don't blame 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).

  • 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:

// 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']); }
Share Improve this question edited Oct 24, 2013 at 14:38 Michael Paulukonis asked Oct 23, 2013 at 14:36 Michael PaulukonisMichael Paulukonis 9,1115 gold badges51 silver badges68 bronze badges 3
  • If you want to do linq stuff in js, this is a good lib jslinq.codeplex. – pax162 Commented Oct 23, 2013 at 14:40
  • Thanks, @pax162 - this is the only LINQ-like function I need, so I don't want to add in a plete library just yet. Do I even need a LINQ-like function? – Michael Paulukonis Commented Oct 23, 2013 at 14:43
  • @pax162 also, that lib only works against arrays, not objects. If I were using an array, I could just access item[0]. – Michael Paulukonis Commented Oct 23, 2013 at 14:52
Add a ment  | 

1 Answer 1

Reset to default 3

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.

发布评论

评论列表(0)

  1. 暂无评论