Last active
March 6, 2023 18:17
-
-
Save shahali007/01b85bbc5450c2105527bef8dde39dd8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| |====================================================== | |
| | Find timezone from browser | |
| |====================================================*/ | |
| Intl.DateTimeFormat().resolvedOptions().timeZone; | |
| /* | |
| |====================================================== | |
| | Convert number into suffix number | |
| | Ex: 1K, 1.04M | |
| |====================================================*/ | |
| function abbreviateNumber(value) { | |
| let newValue = value; | |
| if(value < 0){ | |
| newValue = Math.abs(value); | |
| } | |
| const suffixes = ["", "K", "M", "B","T"]; | |
| let suffixNum = 0; | |
| while (newValue >= 1000) { | |
| newValue /= 1000; | |
| suffixNum++; | |
| } | |
| newValue = newValue.toPrecision(3); | |
| newValue += suffixes[suffixNum]; | |
| if(value < 0){ | |
| return '-'+newValue | |
| } | |
| else{return newValue}; | |
| } | |
| /* | |
| |===================================================== | |
| | Find the hostname with protocol(http, https) | |
| | Output: 'https://gist.github.com' | |
| |===================================================*/ | |
| const urlArr = window.location.href.split('/'); | |
| const host = urlArr[0] + "//" + urlArr[2] | |
| /* | |
| |====================================================== | |
| | Check first array values are exists in second array | |
| |====================================================*/ | |
| let x = [2,4,1]; | |
| let y = [6,5]; | |
| let z = [6,2,1,5]; | |
| const arrayEquals = (a, b) => { | |
| let returnResult = false; | |
| for(let i=0; i<a.length; i++){ | |
| if(b.includes(a[i])){ | |
| returnResult = true; | |
| } | |
| else{ | |
| returnResult = false; | |
| break; | |
| } | |
| } | |
| return returnResult; | |
| }; | |
| arrayEquals(x,z); // false | |
| arrayEquals(y,z); // true | |
| /* | |
| |====================================================== | |
| | Find duplicate value in an array | |
| |====================================================*/ | |
| const toFindDuplicates = (arr) => { | |
| let arry = arr; | |
| let resultToReturn = false; | |
| for (let i = 0; i < arry.length; i++) { | |
| for (let j = 0; j < arry.length; j++) { | |
| if (i !== j) { | |
| if (arry[i] === arry[j]) { | |
| resultToReturn = true; | |
| break; | |
| } | |
| } | |
| } | |
| if (resultToReturn) { | |
| break; | |
| } | |
| } | |
| return resultToReturn; | |
| } | |
| toFindDuplicates([1,2,11,1,21,1]); // true. Duplicate found | |
| toFindDuplicates([1,2,11,21]); // false. Duplicate not found | |
| /* If you are using ES6/ES2015 or later you can do it this way: | |
| |====================================================== | |
| | Find unique object from array | |
| |====================================================*/ | |
| const arr = [ | |
| {name:"Shah Ali" role:"Frontend Developer"}, | |
| {name:"Shah Ali" role:"Senior Frontend Developer"}, | |
| {name:"Jonayed Islam" role:"Web Developer"}, | |
| ]; | |
| const uniqueObj = [...new Set(response.data.map((item) => item.name))]; | |
| Output: [ | |
| {name:"Shah Ali"}, | |
| {name:"Jonayed Islam"}, | |
| ]; | |
| /* | |
| |====================================================== | |
| | Check empty object | |
| |====================================================*/ | |
| const isObjectEmpty = (object) => { | |
| for (const property in object) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| isObjectEmpty({}) // false | |
| isObjectEmpty({name:'javascript'}) // true | |
| /* | |
| |===================================================== | |
| | Check if 'key' is exists in object | |
| | Check null if 'key' exists | |
| | Also check undefined if 'key' exists | |
| |===================================================*/ | |
| 'key' in object && | |
| object.key !== null && | |
| object.key !== undefined | |
| /* | |
| |====================================================== | |
| | Remove multiple comma | |
| |====================================================*/ | |
| // While string | |
| let text = "1,2,,4,,5,"; | |
| const myArray = text.split(",").filter(item=> item !== ""); | |
| console.log(myArray.join(",").toString()) // 1,2,4,5 | |
| // While array | |
| const finalSelcted = ['1', '2', '', '4', '', '5', '']; | |
| const finalSelctedPriority = finalSelcted.filter(item=> item !== undefined); | |
| console.log(finalSelectedPriority) // ['1', '2', '4', '5'] | |
| console.log(finalSelectedPriority.join(",").toString()) // 1,2,4,5 | |
| /* | |
| |====================================================== | |
| | How to Disable the ENTER Key in | |
| | React Material UI Autocomplete form | |
| |====================================================*/ | |
| <Autocomplete | |
| ... | |
| renderInput={(params) => ( | |
| <TextField | |
| {...params} | |
| ... | |
| inputProps={{ | |
| ...params.inputProps, | |
| onKeyDown: (e) => { | |
| if (e.key === 'Enter') { | |
| e.stopPropagation(); | |
| } | |
| }, | |
| }} | |
| /> | |
| )} | |
| ... | |
| ... | |
| ... | |
| /> | |
| /* | |
| |====================================================== | |
| | Group an array of objects by a property value: | |
| | Write a function that takes an array of objects and groups them by a specified property value. | |
| | For example, given an array of objects like { name: "Alice", age: 30 }, { name: "Bob", age: 20 }, { name: "Charlie", age: 30 }, you could group by age to get the output { "20": [{ name: "Bob", age: 20 }], "30": [{ name: "Alice", age: 30 }, { name: "Charlie", age: 30 }] }. | |
| |====================================================*/ | |
| const people = [ | |
| { name: "Alice", age: 30 }, | |
| { name: "Bob", age: 20 }, | |
| { name: "Charlie", age: 30 } | |
| ]; | |
| const groupedPeople = groupBy(people, "age"); | |
| console.log(groupedPeople); | |
| // Output: { "20": [{ name: "Bob", age: 20 }], "30": [{ name: "Alice", age: 30 }, { name: "Charlie", age: 30 }] } | |
| function groupBy(arr, prop) { | |
| return arr.reduce((groups, item) => { | |
| const groupValue = item[prop]; | |
| if (!groups[groupValue]) { | |
| groups[groupValue] = []; | |
| } | |
| groups[groupValue].push(item); | |
| return groups; | |
| }, {}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment