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

Javascript string to object reference (without eval() or indexes) - Stack Overflow

programmeradmin2浏览0评论

I've seen quite some related questions and google results, but none seem to match my problem.

I'm getting a string "header.h2" which I want to contencate to 'var objects'. So I want objects.header.h2 (which contains more hash data).

However, I don't want to use eval() or the often suggested buttons[] for the obvious reason that buttons[header.h2] won't work and I would need buttons[header][h2].

So how can I remain the object notation, or in the worst case, solve my problem?

I've seen quite some related questions and google results, but none seem to match my problem.

I'm getting a string "header.h2" which I want to contencate to 'var objects'. So I want objects.header.h2 (which contains more hash data).

However, I don't want to use eval() or the often suggested buttons[] for the obvious reason that buttons[header.h2] won't work and I would need buttons[header][h2].

So how can I remain the object notation, or in the worst case, solve my problem?

Share Improve this question asked Apr 13, 2011 at 8:56 Gerben JacobsGerben Jacobs 4,5834 gold badges36 silver badges56 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Just a quick sketch of a possible way:

Your data:

var data = [
    {foo: 1, bar: 2, foobar: [
        'a', 'b', 'c'
    ]},
    {foo: 1, bar: 2, foobar: [
        'd', 'e', 'f'
    ]},
    {foo: 1, bar: 2, foobar: [
        'g', 'h', 'i'
    ]}
];

var accessor = '1.foobar.2';

Using a helper function:

function helper(data, accessor) {
    var keys = accessor.split('.'),
        result = data;

    while (keys.length > 0) {
        var key = keys.shift();
        if (typeof result[key] !== 'undefined') {
            result = result[key];
        }
        else {
            result = null;
            break;
        }
    }

    return result;
}

Or making it available to all objects: (personally, I don't like this...)

Object.prototype.access = function (accessor) {
    var keys = accessor.split('.'),
        result = this;

    while (keys.length > 0) {
        var key = keys.shift();
        if (typeof result[key] !== 'undefined') {
            result = result[key];
        }
        else {
            result = null;
            break;
        }
    }

    return result;
};

Debug output:

console.log(
    helper(data, accessor), // will return 'f'
    data.access(accessor) // will return 'f'
); 

I would create a "populate" method which creates the object per the dot notation string it is given:

var populate = function(obj, str) {
  var ss=str.split(/\./), len=ss.length, i, o=obj;
  for (i=0; i<len; i++) {
    if (!o[ss[i]]) { o[ss[i]] = {}; }
    o = o[ss[i]];
  }
  return obj;
};

var objects = populate({}, 'header.h2');
objects.header.h2; // => Object {}
populate(objects, 'foo.bar.gah.zip');
objects.foo.bar.gah.zip; // => Object {}

Needs testing but should get you closer to your goal.

发布评论

评论列表(0)

  1. 暂无评论