0

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

var string = 'response.basicInfo.gender';

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

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 是邪恶的......

更新

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

4

1 回答 1

0

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

将点符号的 JavaScript 字符串转换为对象引用

这是完整的解决方案。

// 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;
于 2013-09-24T01:20:19.823 回答