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

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

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


当前回答

在这个回答的背景下: 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数据字符串。(这个变量在会话中存储为json字符串)。

console.log(json_string_object)

->只返回该字符串的表示形式,无法区分是string还是object。

所以为了让它工作,我只需要把它转换回真实对象:

object = JSON.parse(json_string_object);

检查是否在控制台中应用了任何筛选器。它发生在我在chrome控制台。

console.log(anObject)的输出是误导性的;通过单击>展开控制台中显示的对象树,显示的对象的状态才会解析。当你console.log对象时,它不是对象的状态。

相反,尝试console.log(object .keys(config)),甚至console.log(JSON.stringify(config)),您将看到调用console.log时的键或对象的状态。

您将(通常)发现在console.log调用之后添加了键。

我也遇到过类似的问题(在为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);
    }
});

以防这对某人有帮助,我有一个类似的问题,这是因为有人在我正在使用的对象中创建了. tojson的覆盖。所以对象是这样的:

{
  foo: {
         bar: "Hello"
         baz: "World"
       }
}

但是.toJSON()是:

toJSON() {
  return this.foo
}

所以当我调用JSON.stringify(myObject)它返回"{"bar": "Hello", "baz": "World"}"。然而,Object.keys(myObject)显示了“foo”。