2

I have a lua table like

Save = {
["player 1"] = {
    ["show"] = true,
    ["name"] = "user1",
    ["data"] = 56171308,
},
["player 2"] = {
    ["show"] = false,
    ["name"] = "user1",
    ["data"] = 508703367,
},}

And i want to convert it to a js array through an html page to look like

[   ["player 1","user1",56171308],
["player 2","user2",508703367] ]

I have tried to load the lua content on page and remove some elements with this function

<script>
  function myFunction() {
    var str = document.getElementById("file-content").innerHTML;
    var res = str.replace(/Save|show|true|false|name|data|,|{|}|=|"|[[\]]/g, '');
    var temp = new String(res);
    temp = temp.replace(/^\s*[\r\n]/gm, '');
    document.getElementById("file-content").innerHTML = temp;
  }
</script>

1 Answer 1

1

Here is updated answer for you issue:

function myFunction() {
    var str = document.getElementById("file-content").innerHTML;
    var res = str.replace(/Save|show|true|false|name|data|,|{|}|=|"|[[\]]/g, '  '); // extra white space replace
    var temp = new String(res);
    temp = temp.replace(/^\s*[\r\n]/gm, '');
    temp = temp.replace(/(\r\n|\n|\r)/gm, "");
    temp = temp.replace(/\t/g, '');

    var array = temp.split("  ");
    array = array.filter(function(e) {
        return e
    });
    var finalArray = chunkArray(array, 3)
    console.log(finalArray);
    var longstring = convertToSring(finalArray);
    document.getElementById("file-content").innerHTML = longstring.toString();
}

function chunkArray(myArray, chunk_size) {
    var index = 0;
    var arrayLength = myArray.length;
    var tempArray = [];

    for (index = 0; index < arrayLength; index += chunk_size) {
        myChunk = myArray.slice(index, index + chunk_size);
        // Do something if you want with the group
        tempArray.push(myChunk);
    }

    return tempArray;
}

function convertToSring(finalArray) {
    var longstring = "[";
    for (var i = 0; i < finalArray.length; i++) {
        var string = '["' + finalArray[i].join('","') + '"],';
        console.log(string);
        longstring = longstring + string;
    }
    longstring = longstring.replace(/,\s*$/, ""); // remove last comma
    longstring = longstring + "]";
    return longstring;
}

myFunction();

Check the jsfiddle working code : https://jsfiddle.net/gcfs0kda/2/

Sign up to request clarification or add additional context in comments.

3 Comments

You can see here to code i have changed that scriipt but the output is [["player 1", "user1", "56171308", "player 2", "user1", "508703367"]] and i want to be [["player 1", "user1", "56171308"], ["player 2", "user1", "508703367"]] Livecode
Okay @H0lla8ack will solve your issue and get back to you
Hello @H0lla8ack, I have updated my code as per your requirement please check. Also mark this answer as correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.