简体   繁体   English

无法访问对象的属性?

[英]Can't access properties of an object?

I'm reading a sheet using the SheetJS (xlsx) extension to read from the excel file file like so:我正在使用 SheetJS (xlsx) 扩展读取工作表以从 excel 文件文件中读取,如下所示:

var workbook = XLSX.readFile(req.file.path);
var sheet_name_list = workbook.SheetNames;
let sheet_json = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);

When I run console.log(sheet_json) I get:当我运行console.log(sheet_json)我得到:

[
  {
    'Question*': 'Question 1',
    'Number of Choices*': 2,
    'Choice A*': 'Apple',
    'Choice B*': 'Pineapple',
    'Answer (A/B/C/D)*': 'A',
    'Correct Marking*': 1,
    'Negative marking*': 0
  },
  {
    'Question*': 'Question 2',
    'Number of Choices*': 3,
    'Choice A*': 'Car',
    'Choice B*': 'Bike',
    'Choice C': 'Truck',
    'Answer (A/B/C/D)*': 'C',
    'Correct Marking*': 1,
    'Negative marking*': 1
  }
]

Now I loop over the sheet_json array and I can't validate the properties, the result is like this:现在我遍历 sheet_json 数组,但无法验证属性,结果如下:


// i is the index of iterator. o is the object iterated


   let sn = ["Question*", "Number of Choices*"]
   let props = Object.keys(sheet_json[i]);
   console.log(props); //===> false
   console.log(props.findIndex(o=>o==sn[1])) //===> false
   console.log(o.hasOwnProperty(sn[1])); //===> false
   console.log("Number of Choices*" in o); //===> false
   console.log(sn[1] in sheet_json[i]); //===> false
   console.log(props.includes(sn[1].trim().toString())); //===> false
   console.log(o["Number of Choices*"]); //===> undefined
   console.log(o[sn[1]]!==undefined);  //===> false

Why is everything showing up as undefined or false?为什么一切都显示为 undefined 或 false? Please help.请帮忙。


node -v : v13.14.0 node -v :v13.14.0

npm -v : 6.14.4 npm -v : 6.14.4

xlsx : 0.16 xlsx :0.16


Here's the excel file as well: DOWNLOAD这也是excel文件:下载

I tried replicating it here https://jsfiddle.net/t4ocdw67/ .我尝试在这里复制它https://jsfiddle.net/t4ocdw67/

sheet_json=[{...},{...}];
for(i in sheet_json)
{
    let o = sheet_json[i];
    let sn = ["Question*", "Number of Choices*"]
    .... //more code here
}

It seems to be working fine, there might be a problem with iteration part maybe似乎工作正常,可能是迭代部分有问题

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

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