Home > Software engineering >  How can I append to an empty two dimensional array in Javascript
How can I append to an empty two dimensional array in Javascript

Time:10-20

Here I am reading data from a text file where rows are delimited by string OUT_ and columns are delimited by \n

I am filtering data and throwing out any arrays <9 in col length and not having % at col 6

Then I want to sort alphabetically by col 0, and group all arrays with the same name in col 0

Returning a triple array

        async function fetchText() {
            let response = await fetch('http://127.0.0.1:8080//data.txt');

            let dataOut = await response.text();
            var n = dataOut.split("OUT_");
            var lastTester = "";
            var new_array = [[[]]];
            for (var x in n) {
                var m = n[x].split("\n");
                var temp_array = [[]];
                if (m.length > 9)
                    if (m[6].indexOf('%') > -1) {

                        temp_array.push(m);
                        
                        if (lastTester !== m[0]) {
                            new_array.push(temp_array);
                            temp_array = [[]];
                            document.write("Appended ");
                        }
                        lastTester = m[0];
                    }
            }
            return new_array;
        }
        tester_array = fetchText();
        document.write(tester_array[1][1][0]);
        document.write(tester_array.length());

However, I am not getting any output on either of, it seems tester_array is empty

    document.write(tester_array[1][1][0]);
    document.write(tester_array.length());

It writes Appended to the web page, so it's not a data input or text file problem,

What am I doing wrong with the array creation and appending? What needs to be changed in the code?

The data.txt file looks like this (edited to remove confidential information):

OUT_FusingPower BI
Manufacturing Central
|
Pages
MFG Yield Summary
0
0
0
MFG Yield Trend
MFG Error Details
Yield by Line/Tester
EC FR by Line/Tester
OLR Report
Tester Comparision
Yield by Part Number
Test Time
OUT_Tester by Station
Error Code Details
Yield by Site
About
File
0
0
0
0
OUT_Analytics - Errorcode Failure Rate by Line/Tester
Site
All
Project
Rumba  
0
0
0
All
Station
All
Current Week?
Multiple selections
Shift

CodePudding user response:

your function is async, and you are not waiting for resolving the promise. Therefore, upon accessing tester_array, the promise is not yet fullfiled.

fetchText().then(result => {
  console.log(result[1][1][0])
  console.log(result.length)
})

You can use .then, or you would need to use async/await approach.

async function fetchText() {
    let dataOut = 
        `OUT_FusingPower BI
        Manufacturing Central
        |
        Pages
        MFG Yield Summary
        0
        0
        0
        MFG Yield Trend
        MFG Error Details
        Yield by Line/Tester
        EC FR by Line/Tester
        OLR Report
        Tester Comparision
        Yield by Part Number
        Test Time
        OUT_Tester by Station
        Error Code Details
        Yield by Site
        About
        File
        0
        0
        0
        0
        OUT_Analytics - Errorcode Failure Rate by Line/Tester
        Site
        All
        Project
        Rumba  
        0
        0
        0
        All
        Station
        All
        Current Week?
        Multiple selections
        Shift`;
    var n = dataOut.split("OUT_");
    var lastTester = "";
    var new_array = [[[]]];
    for (var x in n) {
        var m = n[x].split("\n");
        var temp_array = [[]];
        if (m.length > 9)
            if (m[6].indexOf('%') > -1) {

                temp_array.push(m);

                if (lastTester !== m[0]) {
                    new_array.push(temp_array);
                    temp_array = [[]];
                    console.log("Appended");
                }
                lastTester = m[0];
            }
    }
    return new_array;
}

fetchText().then(result => {
    console.log(result[1][1][0]);
    console.log(result.length);
})

  • Related