Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Last active December 16, 2022 06:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nidhi-canopas/30b9a35533d0d2161e56f5d7ae642616 to your computer and use it in GitHub Desktop.
Save nidhi-canopas/30b9a35533d0d2161e56f5d7ae642616 to your computer and use it in GitHub Desktop.
Typescript : Array methods
1. // check whether element exists in an array
function checkIndexOf() {
let emojis:string[] = ['😎️', 'πŸ€“οΈ', '🀩️', 'πŸ˜‡οΈ']
console.log(emojis.indexOf('🀩️') !== -1)
}
// output:
true
2. // find last index of an element inside an array
function checkLastIndexOf() {
let emojis:string[] = [];
console.log("Last index of 🀩️ before : ", emojis.lastIndexOf('🀩️'))
emojis = ['😎️', 'πŸ€“οΈ', '🀩️', 'πŸ˜‡οΈ']
console.log("Last index of 🀩️ after : ", emojis.lastIndexOf('🀩️'))
}
// output:
Last index of 🀩️ before : -1
Last index of 🀩️ after : 2
3. // merge two arrays
function mergeArrays() {
const emojis_1:string[] = ['😎️', 'πŸ€“οΈ', '🀩️', 'πŸ˜‡οΈ']
let emojis_2:string[] = ['😑️', '😳️', 'πŸ‘ΏοΈ', '😍️']
console.log("result : ", emojis_1.concat(emojis_2))
}
// output:
result : [ '😎️', 'πŸ€“οΈ', '🀩️', 'πŸ˜‡οΈ', '😑️', '😳️', 'πŸ‘ΏοΈ', '😍️' ]
4. // join array elements with specific separator
function joinArraysWithOperator() {
let emojis:string[] = ['😎️', 'πŸ€“οΈ', '🀩️', 'πŸ˜‡οΈ']
console.log("result : ", emojis.join("*"))
}
// output:
result : 😎️*πŸ€“οΈ*🀩️*πŸ˜‡οΈ
5. // push an element to the array
function addElement() {
let emojis:string[] = ['🀩️', 'πŸ˜‡οΈ', '😑️', '😳️']
// push 😍️ to emojis
emojis.push('😍️')
console.log("pushed 😍️ emoji: ", emojis)
// push 😎️ to emojis
emojis.push('😎️')
console.log("pushed 😎️ emoji : ", emojis)
}
// output
pushed 😍️ emoji: [ '🀩️', 'πŸ˜‡οΈ', '😑️', '😳️', '😍️' ]
pushed 😎️ emoji : [ '🀩️', 'πŸ˜‡οΈ', '😑️', '😳️', '😍️', '😎️' ]
6. // removes last element from array
function removeElement() {
let emojis:string[] = ['🀩️', 'πŸ˜‡οΈ', '😑️', '😳️', '😍️', '😎️']
// pop will remove last emoji(😎️) from emojis
emojis.pop()
console.log("popped 😎️ emoji : ", emojis)
// pop will remove last emoji(😍️) from emojis
emojis.pop()
console.log("popped 😍️ emoji: ", emojis)
}
// output
popped 😎️ emoji : [ '🀩️', 'πŸ˜‡οΈ', '😑️', '😳️', '😍️' ]
popped 😍️ emoji: [ '🀩️', 'πŸ˜‡οΈ', '😑️', '😳️' ]
7. // reverse an array
function reverseArray() {
let emojis:string[] = ['😎️', '🀩️', 'πŸ€“οΈ', 'πŸ˜‡οΈ'];
console.log("reverse array : ", emojis.reverse())
}
// output
reverse array : [ 'πŸ˜‡οΈ', 'πŸ€“οΈ', '🀩️', '😎️' ]
8. // shift an element in array
function shiftElement() {
let emojis:string[] = ['😎️', '🀩️', 'πŸ€“οΈ', 'πŸ˜‡οΈ'];
const shiftedEmoji = emojis.shift()
console.log("result array : ", emojis)
console.log("shifted emoji : ", shiftedEmoji)
}
// output
result array : [ '🀩️', 'πŸ€“οΈ', 'πŸ˜‡οΈ' ]
shifted emoji : 😎️
9. // unshift an element to an array
function unShiftElement() {
let emojis:string[] = ['😑️', '😳️', 'πŸ‘ΏοΈ', '😍️']
emojis.unshift('😎️')
console.log("unshifted 😎️ emoji: ", emojis)
}
// output
unshifted 😎️ emoji: [ '😎️', '😑️', '😳️', 'πŸ‘ΏοΈ', '😍️' ]
10. // get specific section of the array
function trimArray() {
let emojis: string[] = [ '😎️', 'πŸ€“οΈ', '🀩️', 'πŸ˜‡οΈ', '😑️', '😳️', 'πŸ‘ΏοΈ']
console.log("result : ", emojis.slice(2, 5))
}
// output
result : [ '🀩️', 'πŸ˜‡οΈ', '😑️' ]
11. // add/replace/remove element from an array
function updateArray() {
let emojis: string[] = [ '😎️', '🀩️', 'πŸ˜‡οΈ', 'πŸ‘ΏοΈ']
// add an element to the array
emojis.splice(4, 0, '😑️')
console.log("added 😑️ emoji : ", emojis)
// replace an element to the array
emojis.splice(1, 1, 'πŸ€—οΈ')
console.log("replaced 🀩️ with πŸ€—οΈ emoji : ", emojis)
// remove one or more elements from an array
emojis.splice(2, 2)
console.log("removed πŸ˜‡οΈ and πŸ‘ΏοΈ emojis : ", emojis)
}
// output
added 😑️ emoji : [ '😎️', '🀩️', 'πŸ˜‡οΈ', 'πŸ‘ΏοΈ', '😑️' ]
replaced 🀩️ with πŸ€—οΈ emoji : [ '😎️', 'πŸ€—οΈ', 'πŸ˜‡οΈ', 'πŸ‘ΏοΈ', '😑️' ]
removed πŸ˜‡οΈ and πŸ‘ΏοΈ emojis : [ '😎️', 'πŸ€—οΈ', '😑️' ]
12. // convert an array to string
function convertToString() {
let emojis: string[] = [ '😎️', '🀩️', 'πŸ˜‡οΈ', 'πŸ‘ΏοΈ']
console.log("string from array : ", emojis.toString())
}
// output
string from array : 😎️,🀩️,πŸ˜‡οΈ,πŸ‘ΏοΈ
13. // filter an array with specific condition
function filterArray() {
let emojis_1:string[] = ['😊️', '😍️', 'πŸ˜‚οΈ', '😎️', 'πŸ˜‡οΈ', '🀩️']
let emojis_2:string[] = ['πŸ‘ΏοΈ', '😎️', 'πŸ€“οΈ', 'πŸ€“οΈ', 'πŸ‘ΏοΈ', 'πŸ˜‡οΈ', '😎️', 'πŸ‘ΏοΈ', 'πŸ€—οΈ']
// find common items from two arrays
let result = emojis_1.filter(f => emojis_2.indexOf(f) !== -1)
console.log("common emojis between two arrays : ", result);
// remove duplicate items from an array
let distinctArr = emojis_2.filter((item, index) => emojis_2.indexOf(item) === index)
console.log("duplicate items removed array : ", distinctArr)
}
// output:
common emojis between two arrays : [ '😎️', 'πŸ˜‡οΈ' ]
duplicate items removed array : [ 'πŸ‘ΏοΈ', '😎️', 'πŸ€“οΈ', 'πŸ˜‡οΈ', 'πŸ€—οΈ' ]
14. // get new array from given array
function formatArray() {
let numbers: Array<number> = [2.6, 1.3, 4.0]
let result = numbers.map(Math.ceil) // Math.ceil returns smallest max value of given number
console.log("result : ", result)
}
// output
result : [ 3, 2, 4 ]
15. // check if array is satisfying some condition
function isEvenNumber(item) {
return item % 2 == 0;
}
function checkEvenNumber() {
let numbers: Array<number> = [2, 4, 6, 8]
console.log("result : ", numbers.every(isEvenNumber)) // check whether element is even number
}
// output
result : true
16. // Reduce array from the right
function reduceArrayFromRight() {
let numbers: Array<number> = [1, 2, 3, 4]
let result = numbers.reduceRight(function(a, b){ return a + b})
console.log("result: ", result)
}
// output
result: 10
17. // reduce array from left
function reduceArrayFromLeft() {
let numbers: Array<number> = [20, 5, 10]
let result = numbers.reduce(function(a, b){ return a - b})
console.log("result: ", result)
}
// output
result: 5
18. // check for atleast one condition to be true
function check() {
let numbers: Array<number> = [1, 3, 8]
console.log("Atleast one number is even in given array: ", numbers.some(item => item % 2 == 0))
}
// output
Atleast one number is even in given array: true
19. // sort array
function sortArray() {
let numbers: Array<number> = [1, 3, 10, 5, 8]
console.log("sorted array : ", numbers.sort((a, b) => a - b))
}
// output
sorted array : [ 1, 3, 5, 8, 10 ]
20. // fill array with elements
function fillArray() {
const emojis = [ '😎️', '😑️', 'πŸ˜‡οΈ', 'πŸ‘ΏοΈ'];
// fill with 😍️ from position 2 until position 4
console.log("result 1: ", emojis.fill('😍️', 2, 4));
// fill with πŸ˜‡οΈ from position 1
console.log("result 2: ", emojis.fill('πŸ˜‡οΈ', 1));
// fill with πŸ‘ΏοΈ at all positions
console.log("result 3: ", emojis.fill('πŸ‘ΏοΈ'));
}
// output
result 1: [ '😎️', '😑️', '😍️', '😍️' ]
result 2: [ '😎️', 'πŸ˜‡οΈ', 'πŸ˜‡οΈ', 'πŸ˜‡οΈ' ]
result 3: [ 'πŸ‘ΏοΈ', 'πŸ‘ΏοΈ', 'πŸ‘ΏοΈ', 'πŸ‘ΏοΈ' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment