简体   繁体   中英

Can't access object property in Javascript, property key is from FileReader

I'm using FileReader in Javascript in order to create an Object that has the data. The file itself is a textfile, and the function is configured to make a property for that has all the data under a certain section of the file that starts with a bracketed word and splits by newline '\\n', eg:

[chapter]

data, data, data

data2, data2, data2

becomes:

myObject['chapter'] = ['data, data, data', 'data2, data2, data2'];

but when I use the Chrome console (or use console.log) in order to access myObject['chapter'] it returns undefined. Using the console to look at the properties, it shows that the object does have the properties, but with double quotes?

screenshot looks like this

messing around with the console further reveals that this "chapter" is different from all others. Examples:

myObject['chapter'] = 2
//this creates a new property 'chapter', while "chapter" also exists.
Object.keys(myObject)[0] == 'chapter';
//evaluates to false

edit: spelling

function drophandle(e) {
    let reader = new FileReader();
    let data = e.dataTransfer.files[0];

    reader.onload = function (event) {
        let yet = event.target.result.toString();
        let full_arr = yet.split('\n');
        let index = 'file-f'
        items[index] = [];

        for (let line of full_arr) {
            if (line.indexOf('[') != -1) {
                index = line.replace(/"/g, "").replace(/\[/g, "").replace(/\]/g, "").toLowerCase();
                items[index] = [];
                continue;
            };
            items[index].push(line);
        }
        console.log(items['general'])
        for (let t in items) {
            for (let y of items[t]) {
                append(y)
            }
        }
    }
    reader.readAsText(data, 'utf-8');
}
myObject['chapter'] = 2
//this creates a new property 'chapter', while "chapter" also exists.
Object.keys(myObject)[0] == 'chapter';

is m , not M

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