简体   繁体   中英

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:

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:

[
  {
    '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:


// 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? Please help.


node -v : v13.14.0

npm -v : 6.14.4

xlsx : 0.16


Here's the excel file as well: DOWNLOAD

I tried replicating it here 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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