JS引用多层动态对象字符串

时间:2013-09-23 16:14:44

标签: javascript

假设我有一个引用深层Javascript对象的字符串,例如:

var string = 'response.basicInfo.gender';

我想构建一个安全地检查该对象是否存在的函数,方法是将字符串分割为.并构建对象,检查每个级别,然后处理对象的值,如果它确实存在。

var parseHelper = function(response, items) {
  for (item in items) {
    var parts = String(item).split('.');
    for (var i = 0; i < parts.length; ++i) {
      // ... etc. build the string
    }
  }
}

parseHelper(response, {
  'basicInfo.gender': function(val){
    return (val == 'M') ? 'Male' : (val == 'F') ? 'Female' : val;
  },
})

虽然上面的函数不完整,但我们假设我们使用它来构建字符串并检查它们是否存在:

// so let's suppose we build the string:
var builtString = "response['basicInfo']";

// Now we want to check if it exists 
if (response['basicInfo']) {

  // And if we are fine that it exists, go on to the next item
  var builtString = "response['basicInfo']['gender']";

  // etc.
}

我没有构建该函数的问题,我只是不知道如何评估像"response['basicInfo']['gender']"这样的字符串并将其转换为对象的实际引用。我唯一的猜测是eval(),但eval是邪恶的......

更新

我知道你可以通过window['blah']来引用一个全局对象,但我想引用的这个响应对象不在全局范围内,所以我使用this吗?即使我能做到这一点,我如何用多层参考它?

1 个答案:

答案 0 :(得分:0)

加上1到Bergi,他链接到一个有六个链接的页面,其中一个有我适应的答案来解决问题:

Convert JavaScript string in dot notation into an object reference

这是完整的解决方案。

// We want to run a parse function to convert
// the object response.basicInfo.gender (which is 'M')
// to 'Male', etc.

// Sets the value of a string representing a deep object.
setDeep: function(root, path, value) {
  var parts = path.split('.'), obj = root;
  for (var i = 0; i < parts.length - 1; ++i) {
    obj = obj[parts[i]] || { };
  }
  obj[parts[parts.length - 1]] = value;
  return obj;
},

// Gets the value of a string representing a deep object.
getDeep: function(root, path) {
  var parts = path.split('.'), obj = root, target;
  for (var i = 0; i < parts.length; ++i) {
   target = obj[parts[i]];
   if (typeof target == "undefined") return void 0;
   obj = target;
  }
  return obj;
},

// Loops through multiple string representations
// of deep objects and runs the values through 
// assigned parsing functions for each of them,
// returning the root object.
parseHelper: function(obj, items) {
  for (item in items) {
    var val = getDeep(obj, item);
    var func = items[item];
    if (val !== undefined) {
      val = func(val);
    }
    setDeep(obj, item, val);
  }
  return obj;
},

// response.basicInfo.gender = 'M';
// response.foo.bar = true;

response = parseHelper(response, {
  'basicInfo.gender': function(val){
    return (val == 'M') ? 'Male' : (val == 'F') ? 'Female' : val;
  },
  'foo.bar': function(val) {
    return (val) ? false : true;
  },
});

// response.basicInfo.gender = 'Male';
// response.foo.bar = false;