Skip to content

Instantly share code, notes, and snippets.

@K3ndev
Last active October 3, 2023 10:23
Show Gist options
  • Save K3ndev/c7400a1854f322bddcee9517cf745890 to your computer and use it in GitHub Desktop.
Save K3ndev/c7400a1854f322bddcee9517cf745890 to your computer and use it in GitHub Desktop.

immutable

  • ES5 (2009)

some()

const bool = arr.some(element => element > 3);
console.log(bool); // Output: true

every()

const bool = arr.every(element => element > 0);
console.log(bool); // Output: true

map()

const newArray = arr.map(element => element * 2);
console.log(newArray); // Output: [2, 4, 6, 8, 10]

reduce()

const value = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(value); // Output: 15

indexOf()

const index = arr.indexOf(3);
console.log(index); // Output: 2

filter()

const newArray = arr.filter(element => element > 2);
console.log(newArray); // Output: [3, 4, 5]

concat()

const newArray = arr.concat([6, 7, 8]);
console.log(newArray); // Output: [1, 2, 3, 4, 5, 6, 7, 8]

slice()

const newArray = arr.slice(1, 4);
console.log(newArray); // Output: [2, 3, 4]
  • ES2015 (2015)

find()

const foundElement = arr.find(element => element > 3);
console.log(foundElement); // Output: 4

findIndex()

const index = arr.findIndex(element => element > 3);
console.log(index); // Output: 3
  • ES2019 (2019)

flat()

const newArray = arr.flat();
console.log(newArray); // Output: [1, 2, 3, 4, 5]

flatMap()

const newArray = arr.flatMap(element => [element, element * 2]);
console.log(newArray); // Output: [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]

mutable

  • ES5 (2009)

push()

arr.push(6);
console.log(arr); // Output: [1, 2, 3, 4, 5, 6]

pop()

const removedElement = arr.pop();
console.log(removedElement); // Output: 5

shift()

const removedElement = arr.shift();
console.log(removedElement); // Output: 1

unshift()

arr.unshift(0);
console.log(arr); // Output: [0, 2, 3, 4, 5]

reverse()

const reversedArray = arr.reverse();
console.log(reversedArray); // Output: [5, 4, 3, 2, 0]

sort()

const sortedArray = arr.sort();
console.log(sortedArray); // Output: [0, 2, 3, 4, 5]

splice()

const removedElements = arr.splice(1, 2);
console.log(removedElements); // Output: [2, 3]
  • ES2015 (2015)

copyWithin()

arr.copyWithin(0, 2);
console.log(arr); // Output: [3, 4, 5, 3, 4]

fill()

const filledArray = arr.fill(0, 2, 4);
console.log(filledArray); // Output: [1, 2, 0, 0, 5]
  • ES2019 (2019)

flat()

const newArray = arr.flat();
console.log(newArray); // Output: [1, 2, 3, 4, 5]

flatMap()

const newArray = arr.flatMap(element => [element, element * 2]);
console.log(newArray); // Output: [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]

-- notes: Immutability has a performance cost, so use mutability only when necessary.

search query

type dataType = {
	name: string
}

const data  = [
	{
		name : 'foo1'
	},
	{
		name : 'foo2'
	},
	{
		name : 'foo2'
	},
	{
		name : 'foo1'
	},
	{
		name : 'foo1'
	},
]

const search = (target: string, arr: dataType[]) => {
    return arr.filter(str => str.name.includes(target));
}

console.log(search('foo', data))

looping an object

const obj = { a: 1, b: 2, c: 3 };

for (const [key, value] of Object.entries(obj)) {
  console.log(key, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment