Last active
December 16, 2022 06:38
-
-
Save nidhi-canopas/30b9a35533d0d2161e56f5d7ae642616 to your computer and use it in GitHub Desktop.
Typescript : Array methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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