检索 JS' 的对象值,而不知道它的名称
Retrieving JS' Object value without knowing it's name
这是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
和IfUndefined
分别是要调用的函数的命题,如果当前指向的 PropertyName 存在,否则,我想调用这些函数,同时提供它们作为参数,非常objCheck.PropertyName
的值,以便在返回给用户之前可以对其进行处理。
我将给出一个使用示例:
我将向这个函数提供我从外部提供程序(例如,外部 JSON 返回 WebService(收到的对象,从中我知道一些可能定义也可能未定义的属性。例如,此对象可以是:
var userData1 = {
userID : 1
userName: "JoffreyBaratheon",
cargo: "King",
age: 12,
motherID : 2,
//fatherID: 5,--Not defined
Status: Alive
}
或
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(PropertyValue);
而不是properties[x].ifDefined();
的情况下才能真正工作.最后,我的问题就在这里了。
在一致性检查函数中,我只知道给定属性的名称(如果提供了(。在里面,我不能简单地称它为价值,因为没有properties[x].ifUndefined(properties[x].GetValueFromProperty(properties[x].PropertyName))
,...这样的功能。有吗?
对不起。我的母语不是英语(我是巴西人(,我无法用简短的方式正确表达我的疑虑,所以我宁愿花时间写一篇长篇文章,(希望不要浪费(试图让它更清楚。
如果,即便如此,我的疑问不清楚,请告诉我。
我想你在这里寻找括号符号。它允许您提供任意值作为访问对象的键。另外,你知道它的名字。你有你的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
是一种数组方法,允许您循环它们。不过,您仍然可以对常规循环执行相同的操作。
- 在不知道深度或父属性的情况下从对象中删除属性
- 如何在不知道关键字的情况下访问javascript对象值
- 我不知道如何正确地将REST响应对象传递给ReactJS子组件
- 在不知道名称的情况下访问 json 对象中的元素
- 当我不知道密钥的名称时,如何读取 JSON 对象
- 在不知道键的情况下从对象获取值
- 如何构建数据对象以在不知道多少时传递给 ajax
- 当我不知道属性名称时,如何从 javascript 对象中获取值
- 在 IE10 中访问对象的“文档”成员,而不知道其类型
- 如何在不知道名称的情况下调用javascript匿名对象
- 检索 JS' 的对象值,而不知道它的名称
- 不知道如何在Javascript/jQuery中解析这个PHP/JSON对象
- 如何从变量中清除以前的数据如果我不'不知道变量的类型(可以是数组、对象或基元)
- Javascript:在数组中查找对象的索引,不知道索引,只知道对象
- 如何在不知道对象名称的情况下返回Json对象子对象
- 如何在不知道其位置的情况下删除Mongo对象(嵌套的2个数组)
- 访问JavaScript'的对象属性,而不知道该属性名称
- JavaScript为嵌套对象中的元素赋值而不知道级别
- 从对象数组中获取键值数组,而不知道对象数组的格式(Javascript)
- Json在不知道对象名称的情况下获取对象