Article image
Jorge Silva
Jorge Silva31/05/2022 01:51
Compartilhe

Cheat sheet de métodos de arrays

    const items = [1, 2, 3, 4];

    items.at(1); //2

    items.pop(); //[1, 2, 3]

    items.push(4, 5); //[1, 2, 3, 4, 5]

    items.join("-"); //[1, 2, 3, 4, 5] -> '1-2-3-4-5'

    items.shift(); //[2, 3, 4, 5]

    items.reverse(); //[5, 4, 3, 2]

    items.reverse(); //[2, 3, 4, 5]

    items.unshift(1); //[1, 2, 3, 4, 5]

    items.includes(2); //true

    items.includes(99); //false

    items.map((num) => { return num * 2 }); //map verboso

    items.map(num => num * 2); //map compacto

    items.some(num => num > 3); //true

    items.find(num => num > 2); //3

    items.every(num => num > 0); //true

    items.filter(num => num > 2); //[3, 4, 5]

    items.findIndex(num => num > 2); //2

    items.reduce((acc, num) => acc * num); //120

    items.fill(1); //[1, 1, 1, 1, 1]

    Compartilhe
    Comentários (0)