简体   繁体   English

如何访问包含点的变量?

[英]How do I access a variable which contains a dot?

Following is my script以下是我的脚本

var err=activityLocal.message;
var text="";
var i;
    for (i = 0; i < err.length; i++) {
            text+= err[i].message +";";
}
document.write(text);

Here the value of activityLocal.message as shown below:这里activityLocal.message的值如下图所示:

[{
  "message": "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'manaed_server_4'.",
  "severity": "FAILURE"
}, {
  "message": "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'managed_server_3'.",
  "severity": "FAILURE"
}];

But the output is undefined , seems like JS engine gets confused with the dot in activityLocal.message variable.但是输出是undefined ,似乎 JS 引擎与activityLocal.message变量中的点混淆了。 How can I resolve this error?如何解决此错误?

In your code their is problem with variable name it should not contain dot it is illegal .在您的代码中,它们是变量名的问题,它不应该包含点,这是非法的。

var activityLocal_message = [{
  "message": "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'manaed_server_4'.",
  "severity": "FAILURE"
}, {
  "message": "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'managed_server_3'.",
  "severity": "FAILURE"
}];

var err=activityLocal_message;
var text="";
var i;
    for (i = 0; i < err.length; i++) {
            text+= err[i].message +";";
            //console.log(err[i].message);
}
console.log(text);

or else if you really want to use with dot operator try this或者如果你真的想使用点运算符试试这个

You can't use variables in dot notation (short of using eval, which you don't want to do).您不能在点表示法中使用变量(缺少使用 eval,您不想这样做)。 With dot notation the property name is essentially a constant.使用点表示法,属性名称本质上是一个常数。

myObj.propName
// is equivalent to
myObj["propName"]

You can use array map function to loop through all messages in the activityLocal.message which will return array of strings and the join them using array join function like this:您可以使用数组map函数循环遍历activityLocal.message中的所有消息,这将返回strings数组并使用数组join函数将它们连接起来,如下所示:

 var err= [{ "message": "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'manaed_server_4'.", "severity": "FAILURE" }, { "message": "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'managed_server_3'.", "severity": "FAILURE" }]; var text= err.map(function(e) { return e.message; }).join(";"); alert(text);

The array is able loop the values but the problem might be setting it in the html.该数组能够循环值,但问题可能是在 html 中设置它。 below is the code and you can also check in the Plunker下面是代码,您也可以签入Plunker


// Code goes here

var activityLocal = {};
activityLocal.message = [
  {
    message:
      "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'manaed_server_4'.",
    severity: 'FAILURE',
  },
  {
    message:
      "Data Source 'TestDataSource' is in a state incompatible with this operation at server 'managed_server_3'.",
    severity: 'FAILURE',
  },
];
var err = activityLocal.message;
var text = '';
var i;
for (i = 0; i < err.length; i++) {
  text += err[i].message + ';';
  //alert(text);
}
document.getElementById('showtext').innerHTML = 'tttt' + text;


If you mean that you want to use the dot as the variable name, it is simply not possible.如果您的意思是要使用点作为变量名,那根本不可能。

If you want to use it as an object variable, it is accessible via the object.variableName .如果您想将其用作对象变量,则可以通过object.variableName访问它。 E. g.例如。 assume your object is named myObj and you variable assigned to that object is called myVar you can access it with this call: myObj.myVar .假设您的对象名为myObj并且分配给该对象的变量名为myVar您可以通过以下调用访问它: myObj.myVar Or you can also iterate over the object using the for (or foreach in modern JavaScript runtime environments) loop.或者,您也可以使用 for(或现代 JavaScript 运行时环境中的 foreach)循环遍历对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM