'How can I get data from tiled map editor to my javascript file?

I have a JavaScript game, using the Phaser 2 framework, and I am trying to add a new map to my game. So I made a scene in Tiled map editor so I can export to my JavaScript file, but when I exported to JavaScript, they put in this format (full file at https://pastebin.com/wke5SKkV):

"data":[0, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, ..., 0, 4, 0]

But I want it in this format:

{
    x: 1,
    y: 0,
    t: 'w'
}

Is there anyway I can export like this?

I hope someone knows how to do this!



Solution 1:[1]

Assuming the map is 63x25, you can map the values in a "matrix" which is nothing more that an array of arrays. The idea is that the array you indicated contains the rows added one after the other(data[0,0] is map[0,0], data[63 * 1 + 0] is map(1, 0) - first element on the second row, and so on).

let data = JSON.parse('{"data" = [...]}').data // parse the string object
let mapped = new Array(25) // initialize 25 rows - an array of size 25 containing no values [undefined, undefined, ...]

for (let y = 0; y < 25; y++){
    mapped[x] = new Array(63) // initialize a new row
    for (let x = 0; x < 63; x++){
        mapped[x, y] = { x: x, y:y, t: data[x*63 + y] }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1