Sources:https://www.w3schools.com/js/js_regexp.asp
1. Convert UTC date into local time zone date.
<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js">moment.utc('2017-04-03 12:43:52.0').local().format('YYYY-MM-DD HH:mm:ss');</script>1.1 Below works in Chrome console (useful to test any library.)
document.write('<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js"></script>');moment.utc('2017-04-03 12:43:52.0').local().format('YYYY-MM-DD HH:mm:ss');document.write('<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>');2. Setting interval & timeouts.
a=5;var numLogger = setInterval(()=>{a+=5; console.log(a);}, 1000);clearInterval(numLogger);var logger=setInterval(function(){console.log("Hello " + new Date().getTime())}, 1000);clearInterval(logger);var timeoutEx = setTimeout(function(){console.log('Hello World! ' + new Date());}, 1000);clearTimeout(timeoutEx);--------------------------EX-------------a=5;var numLogger = setInterval(()=>{a+=5; console.log(a);if(a>20){clearInterval(numLogger); console.log("checking ");}}, 1000);3. Have an Array of Objects, want to extract value of specific property from each Object into an Array.
objectArray=[{'name':'Ajay', age:18}, {'name':'Bob', age:46}, {'name':'Chris', age:27}]ages=objectArray.map(function(entry){return entry.age;})names=objectArray.map(efunction(entry){return entry.name;})4. Array as queue. fifo using shift() & lifo using pop(); shift/unshift works on array "top" ... push/pop works on array "bottom"
var a=['as', 'we', 'cx', 'fg'];a.pop(); //returns last [i.e- rightmost] element 'fg' removing it from a.a.shift(); //returns first [i.e- leftmost] element 'as' removing it from a.a.push("xy"); //adds a new element to right/bottom/last in array a.a.unshift("za"); //adds a new element to left/top/first in array a.document.addEventListener("click", function(event){ alert("Was Clicked!" + document.getElementFromPoint(event.clientX, event.clientY));});5. Search and replace
var str = "Visit W3Schools!";var n = str.search("W3Schools"); //n=66. Loop constructs :
7. Conditional constructs:
8. Get selected text on page:
document.getSelection().anchorNode.data.substr(document.getSelection().anchorOffset, document.getSelection().focusOffset - document.getSelection().anchorOffset);9. Sort given array of objects, based on some property value of objects.
a=[{aq:'sdf', ab:'er', id:2}, {aq:'www', ab:'q1q', id:1}, {aq:'rr', id:5}]b=a.sort(function(a,b){return a.id-b.id});console.log(JSON.stringify(b));e.g- To sort date timea=["2017:09:11 14:39:00", "2017:09:11 14:56:00", "2017:09:11 11:39:00"]a.sort(function(a,b){ar=a.replace(/ /g, '').replace(/:/g, ''); br=b.replace(/ /g, '').replace(/:/g, ''); console.log(ar, br); return ar-br;})#result:[ "2017:09:11 11:39:00", "2017:09:11 14:39:00", "2017:09:11 14:56:00" ]Output - [{"aq":"www","ab":"q1q","id":1},{"aq":"sdf","ab":"er","id":2},{"aq":"rr","id":5}]
10. Delete entry with key = <KEY> from Map<MAPNAME>.
delete <MAPNAME>.<KEY>Maths:
a=44.45897 #How to round this to 2 digit after decimala.toFixed(2) #returns 44.46Object merge or union operation on objects. equivalent to angular.extend(destObj, srcObj1, srcObj...);
a={'asd':12}b={'er':22, 'asd': 67}Object.assign({}, b, a) # Result object will have keys from both object and values of later overrides the previous.Copy by Value (Every value(Object) assignment is by reference. i.e- if you assign 'a'[where a is array] to 'b', then change in either will impact other.) to avoid this we can us e map function on a and store result in b as follows.
a=["2017:09:11 14:39:00", "2017:09:11 14:56:00", "2017:09:11 11:39:00"]b=a.map(function(i){return i;});Have array of object(containing key(s) & value(s)), want to perform some reduce action on each object's specific key's value.
var arr = [{x: 1}, {x:2, y: 5}, {x: 4}]; //sample data, out of which we like to have total of x in each object.arr.reduce(function (a, b) { return {x: a.x + b.x}; // returns object(as total) with property x}); #Returns {x:7}-------------Cookieeee-----src:----w3schools--------
#Setting Cookie
function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";}#Getting Cookie
function getCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for(var i = 0; i <ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return "";}-------------Cookieeee-----src:----w3schools--------