Skip to content

Instantly share code, notes, and snippets.

View IKatsuba's full-sized avatar

Igor Katsuba IKatsuba

View GitHub Profile
const items = [1, 2, 3, 4];
//before
items.sort(() => Math.random() - 0.5);
//after
items.shuffle();
const items = [1, 2, 3, 4];
//before
const index = items.reverse().findIndex(fn); // the reverse method mutates the array
//after
const index = items.findLastIndex(fn);
const items = [1, 2, 3, 4];
//before
const item = items.reverse().find(fn); // the reverse method mutates the array
//after
const item = items.findLast(fn);
const items = [1, 2, 3, 4];
//before
const itemsForDelete = items.filter(item => item % 2 === 0);
itemsForDelete.forEach(item => {
const index = items.indexOf(item);
items.splice(index, 1);
})
//after
items.deleteWhere(item => item % 2 === 0);
const items = [1, 2, 3, 4];
//before
items.splice(index, 1);
//after
items.deleteAt(index);
const items = [1, 2, 3, 4];
//before
const index = items.indexOf(item);
items.splice(index, 1);
//after
items.delete(item);
const items = [1, 2, 3, 4];
//before
items.splice(1, 0, 3);
console.log(items);
//after
items.insert(1, 3);
console.log(items);
const items = [1, 2, 3, 4];
//before
const two = items[1];
const three = items[2];
//after
const two = items.get(1);
const three = items.get(2);
const items = [1, 2, 3, 4];
items.clear();
console.log(items);
const items = [1, 2, 3, 4];
items.splice(0, items.length);
//or
while(items.length > 0) {
items.pop();
}