由于键名复杂,访问JavaScript数组成员时出现问题
我有一个数组: 第一个键是由于键名复杂,访问JavaScript数组成员时出现问题,javascript,Javascript,我有一个数组: 第一个键是”http://schemas.microsoft.com/ws/2008/06/identity/claims/role“的值为“Admin” 我想访问此密钥,但使用密钥名称时遇到问题,因为它是一个href而不是一个名称 如何访问值“Admin”?我想使用saymyArray.role等,但名称role位于一个非常长的href的末尾,带有许多斜杠等 我想知道访问它的最佳方式,但在StackOverflow上找不到任何内容。基于密钥名进行访问会很好。以下是一种方法:
”http://schemas.microsoft.com/ws/2008/06/identity/claims/role“
的值为“Admin”
我想访问此密钥,但使用密钥名称时遇到问题,因为它是一个href而不是一个名称
如何访问值“Admin”
?我想使用saymyArray.role
等,但名称role位于一个非常长的href
的末尾,带有许多斜杠等
我想知道访问它的最佳方式,但在StackOverflow上找不到任何内容。基于密钥名进行访问会很好。以下是一种方法:
var obj={
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role“:“管理员”,
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name“:“管理员”,
“nbf”:1512187433,
“经验”:1512187453,
“iss”:“超级WesomeTokenServer”,
“澳元”:http://localhost:3000/"
}
console.log(obj[”http://schemas.microsoft.com/ws/2008/06/identity/claims/role"]);代码>当且仅当标识符不包含任何特殊字符(除ux外)时,可使用obj.key
执行此操作。因此,如果希望获得URI的结果(如键),可以使用数组键表示法获取数据
var variable = {
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "Admin",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "admin",
"nbf": 1512187433,
"exp": 1512187453,
"iss": "SuperAwesomeTokenServer",
"aud": "http://localhost:3000/"
};
//When key heaving the special char can be done like only.
console.log(variable["http://schemas.microsoft.com/ws/2008/06/identity/claims/role"]); // Admin
// But if the key isn't contained special char that can be used in both ways.For Example:
console.log(variable["nbf"]); // 1512187433
console.log(variable.nbf); // 1512187433
这对我很有用:obj[”http://schemas.microsoft.com/ws/2008/06/identity/claims/role“]
如果它是数组,就不能使用点符号myArray.role
,这只是当你进入一个对象时,对于数组,你要用括号表示,比如@Mark\M说obj[”http://schemas.microsoft.com/ws/2008/06/identity/claims/角色“]
这对第一个或第二个键/值不起作用。这可能是因为键名中有前后斜杠。是的,我在回答中也强调了这一点。
var variable = {
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "Admin",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "admin",
"nbf": 1512187433,
"exp": 1512187453,
"iss": "SuperAwesomeTokenServer",
"aud": "http://localhost:3000/"
};
//When key heaving the special char can be done like only.
console.log(variable["http://schemas.microsoft.com/ws/2008/06/identity/claims/role"]); // Admin
// But if the key isn't contained special char that can be used in both ways.For Example:
console.log(variable["nbf"]); // 1512187433
console.log(variable.nbf); // 1512187433