Skip to content
hazzard993 edited this page Mar 4, 2019 · 2 revisions

Object and array iteration in TypeScript.

Iterate through an object's keys and values

let object = { x: 0, y: 0 };
for (const key in object) {
    const value = object[key];
    print(key, value);
}

Iterate over an array

let array = [0, 1, 2];
for (const item of array) {
    print(item);
}

or

let array = [0, 1, 2];
array.forEach(num => print(num));
array.map(num => print(num));
array.filter(num => print(num));

The latter option uses LuaLibs

Iterate over a coroutine

Be sure not to name your coroutine "coroutine"

function * testCoroutine() {
    yield [1, 2];
    yield [3, 4];
    yield [5, 6];
}

for (const [x, y] of testCoroutine()) {
    print(x, y);
}
Clone this wiki locally