Lua table 转js数组

Lua table to js array

我有一个 lua table 喜欢

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

我想通过 html 页面将其转换为 js 数组,看起来像

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

我已尝试使用此功能加载页面上的 lua 内容并删除一些元素

<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>

这是针对您问题的更新答案:

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();

检查 jsfiddle 工作代码:https://jsfiddle.net/gcfs0kda/2/