在不知道名称的情况下检索JS'对象值

时间:2014-10-11 00:36:45

标签: javascript

这是SO中一个相当常见的问题,在决定提出这个问题之前,我已经研究过很多这样的问题。

我有一个函数,特此称为CheckObjectConsistency,它接收一个参数,一个具有以下语法的对象:

objEntry:
{
        objCheck: anotherObject,
        properties: [
            {
                //PropertyValue: (integer,string,double,whatever), //this won't work.
                PropertyName: string,
                ifDefined: function,
                ifUndefined: function
            }
            ,...
        ]
}

这个函数的作用是......考虑到给定的参数设计正确,它得到了包含在其中的objCheck(var chk = objEntry.objCheck;),然后它会检查它是否包含properties中包含的for(x=0;x<=properties.length;x++){ if(objCheck.hasOwnProperty(properties[x].PropertyName)){ properties[x].ifDefined(); } else{ properties[x].ifUndefined(); } 这个系列。

喜欢这个

IfDefined

我想要的是......我想把它带到另一个动态水平:给定IfUndefinedobjCheck.PropertyName分别是被调用函数的命题,如果当前指向的话PropertyName存在,否则,我想调用这些函数,同时作为参数提供非常var userData1 = { userID : 1 userName: "JoffreyBaratheon", cargo: "King", age: 12, motherID : 2, //fatherID: 5,--Not defined Status: Alive } 的值,以便在返回给用户之前对其进行处理。

我将举一个用法示例:

我将为此函数提供一个从外部提供程序(例如,外部JSON-returning-WebService)收到的对象,我从中了解了一些可能定义或未定义的属性。 例如,此对象可以是:

var userData2 = {
    userID : 
    userName: "Gendry",
    cargo: "Forger Apprentice",
    //age: 35, -- Not Defined
    //motherID: 4,-- Not Defined
    fatherID: 3,
    Status:    Alive
}

var objEntry=
{
        objCheck: userData1,
        properties: [
            {
                PropertyName: "age",
                ifDefined: function(val){alert("He/she has an age defined, it's "+val+" !");},
                ifUndefined:  function(){alert("He/she does not have an age defined, so we're assuming 20.");},
            },
            {
                PropertyName: "fatherID",
                ifDefined: function(val){alert("He/she has a known father, his ID is "+val+" !");},
                ifUndefined:  function(){alert("Oh, phooey, we don't (blink!blink!) know who his father is!");},
            }

        ]
}

CheckObjectConsistency(objEntry); // Will alert twice, saying that Joffrey's age is 12, and that his father is supposedly unknown.

我的功能将收到:

ifDefined

properties[x].ifDefined();只有在我以某种方式提供properties[x].ifDefined(PropertyValue);而不是properties[x].ifUndefined(properties[x].GetValueFromProperty(properties[x].PropertyName))时才会真正有用。最后,这是我的问题。

内部一致性检查功能中,我只知道给定属性的名称。在里面它,我不能简单地称它为值,因为没有{{1}}这样的功能,......有吗?

对不起不是母语为英语的人(我是巴西人),我无法在短时间内恰当地表达我的怀疑,所以我更愿意花时间写一篇长篇文章,希望(希望不要浪费)试图让它更清晰

即使如此,我的疑问还不清楚,请告诉我。

1 个答案:

答案 0 :(得分:1)

我认为你在这里寻找括号表示法。它允许您提供任意值作为访问对象的键。另外,你知道它的名字。您有properties个对象吗?

objEntry.properties.forEach(function(property){

  // Check if objCheck has a property with name given by PropertyName
  if(!objEntry.objCheck.hasOwnProperty(property.PropertyName)){

    // If it doesn't, call isUndefined
    property.isUndefined();
  } else {

    // If it does, call isDefined and passing it the value
    // Note the bracket notation, allowing us to provide an arbitrary key
    // provided by a variable value to access objCheck which in this case is
    // the value of PropertyName
    property.isDefined(objEntry.objCheck[property.PropertyName]);
  }
});

哦,是的,forEach是一种数组方法,允许你循环遍历它们。您仍然可以使用常规循环执行相同操作。