下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。Col_id_3(见截图中的“undefined”?)有人能解释一下吗?我也可以访问除field_id_4之外的所有其他属性。

console.log(config);
console.log(config.col_id_3);

这就是这些行在控制台中打印的内容


当前回答

好吧,我经过了一个让我对新循环感到害怕的情况,感谢我的老派。

forEach循环和for(a in obj)循环都会产生错误的信息和错误,这取决于对象的类型和属性!!

老的好

for(var i=0, max=arr.length; i<max; i++)
{ //Properties available correctly!

   arr[i].property        //both work inside old school loop!
   arr[i][property]  
}             

其他回答

我今天也遇到了同样的问题。在我的例子中,键是嵌套的,即key1.key2。 我使用split()分割键,然后使用方括号符号,这对我来说很有效。

var data = {
    key1: {
          key2: "some value"
       }
}

我把键分开并像这样使用它,data[key1][key2]为我做了这项工作。

好吧,我经过了一个让我对新循环感到害怕的情况,感谢我的老派。

forEach循环和for(a in obj)循环都会产生错误的信息和错误,这取决于对象的类型和属性!!

老的好

for(var i=0, max=arr.length; i<max; i++)
{ //Properties available correctly!

   arr[i].property        //both work inside old school loop!
   arr[i][property]  
}             

我也遇到过类似的问题(在为SugarCRM开发游戏时),我的出发点是:

var leadBean = app.data.createBean('Leads', {id: this.model.attributes.parent_id});

// This should load object with attributes 
leadBean.fetch();

// Here were my attributes filled in with proper values including name
console.log(leadBean);

// Printed "undefined"
console.log(leadBean.attributes.name);

问题是在fetch(),它的异步调用,所以我必须重写我的代码:

var leadBean = app.data.createBean('Leads', {id: this.model.attributes.parent_id});

// This should load object with attributes 
leadBean.fetch({
    success: function (lead) {
        // Printed my value correctly
        console.log(lead.attributes.name);
    }
});

在这个回答的背景下: https://stackoverflow.com/a/52994419

重新生成文件的解决方案可能并不总是可行的。去除字符串前的“非空格”字符。所以,如果你能修剪字段名,就能把事情清理干净。

就像这样:

const records = parse(csvData, {
      columns: (colNameArray) => {
        return colNameArray.map((value, index) => {          
          return index === 0 ? value.trim() : value;
        });
      },
      onRecord: mapper.mapRow
    });

如果您不关心这个的性能,也可以跳过索引检查。

我也有同样的问题。我的解决方案是使用字符串化输出作为解析JSON的输入。这对我很管用。希望对你有用

var x =JSON.parse(JSON.stringify(obj));
console.log(x.property_actually_now_defined);