DEV Community

Miguel Ramirez
Miguel Ramirez

Posted on

Javascript [array methods] - findIndex, reverse, sort, join, values, unshift, shift, push, pop

const items: any[] = [1,5,2,3,4]
const userData = ["Carlos", "Perez", "12 años"]  
console.log(`
[reversed]: ${items.reverse()}
[sorted]: ${items.sort()}
[foundIndex]: ${items.findIndex((i) => i > 2)}
[join]: ${userData.join("<>")}
[values] new iterator used in forOf: ${items.values()}
[unshift] new lenght: ${items.unshift("a", "b")}
[unshift] new items: ${items}
[shift] first value: ${items.shift()}
[shift] new items: ${items}
[push] new lenght: ${items.push("new element","new last element")}
[push] new items: ${items}
[pop] last value: ${items.pop()}
[pop] new items: ${items}
`)
Enter fullscreen mode Exit fullscreen mode

result:

[LOG]: "
[join]: Carlos<>Perez<>12 años
[values] new iterator used in forOf: [object Array Iterator]
[unshift] new lenght: 7
[unshift] new items: a,b,1,2,3,4,5
[shift] first value: a
[shift] new items: b,1,2,3,4,5
[push] new lenght: 8
[push] new items: b,1,2,3,4,5,new element,new last element
[pop] last value: new last element
[pop] new items: b,1,2,3,4,5,new element
"

note:

  • unshift/shift are similiar to push/pop [first/last item]

Top comments (0)