JavaScript/CheatSheet: Difference between revisions
(Die Seite wurde neu angelegt: „== Data Types == <pre> var num = 8; // number var strg = "Joe"; // string var vobj = {first: "John", las…“) |
mNo edit summary |
||
(21 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
const pi = 3.14; | const pi = 3.14; | ||
typeof strg // =string | |||
</pre> | |||
== Arrays == | |||
<pre> | |||
var title = new Array(); | |||
title[0] = "Mr."; | |||
title[1] = "Mrs."; | |||
element = title.slice(-1); | |||
</pre> | |||
<pre> | |||
concat() | |||
filter() | |||
forEach() | |||
join() | |||
map() | |||
pop() | |||
push() | |||
reduce() | |||
reverse() | |||
shift() | |||
slice() | |||
splice() | |||
sort() | |||
</pre> | </pre> | ||
== Strings == | == Strings == | ||
<pre> | <pre> | ||
charAt() | |||
charCodeAt() | |||
concat() | |||
fromCharCode() | |||
indexOf() | |||
lastIndexOf() | |||
match() | |||
replace() zpl = zpl.replace("1234567890",msg.serialnumber) | |||
search() | |||
slice() checksum = String(sum).slice(-1) # get last digit | |||
split() | |||
substr() | |||
substring() | |||
toLowerCase() | |||
toUpperCase() | |||
valueOf() | |||
trim() remove whitespace from both sides | |||
trimStart() | |||
trimEnd() | |||
includes() if (string.includes(".prn")) {} | |||
</pre> | |||
* Remove ANSI colors from string | |||
<blockquote> | |||
<pre> | |||
payload = payload.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); | |||
</pre> | |||
</blockquote> | |||
* add leading zeros | |||
<blockquote> | |||
<pre> | |||
var string = "000000" + integer.toString() | |||
string = string.substr(string.length-6) | |||
</pre> | |||
</blockquote> | |||
== Conversions == | |||
<pre> | |||
string = integer.toString() | |||
integer = parseInt(string) + 1 | |||
</pre> | |||
== Functions == | |||
<pre> | |||
function addition(number1, number2) { | |||
return number1 + number2; | |||
} | |||
var person = { | |||
name: 'Peter', | |||
getName: function () { | |||
return this.name; | |||
} | |||
} | |||
person.getName() | |||
</pre> | </pre> | ||
Line 50: | Line 136: | ||
>> Right shift | >> Right shift | ||
>>> Zero fill right shift | >>> Zero fill right shift | ||
</pre> | |||
== if - else == | |||
<pre> | |||
if (condition) { | |||
// if condition is met | |||
} else { | |||
// if condition is not met | |||
} | |||
</pre> | |||
== switch == | |||
<pre> | |||
switch(var) { | |||
case 1: | |||
// code | |||
break; | |||
case 2: | |||
// code | |||
break; | |||
default: | |||
// code | |||
} | |||
</pre> | |||
== Loops == | |||
<pre> | |||
for (var i = 0; i < 10; i++){ | |||
} | |||
while (i < 10) { | |||
i++; | |||
} | |||
do { | |||
i++; | |||
} while (i < 10); | |||
break | |||
continue | |||
</pre> | |||
== Escape Characters == | |||
<pre> | |||
\' Single quote | |||
\" Double quote | |||
\\ Backslash | |||
\b Backspace | |||
\f Form feed | |||
\n New line | |||
\r Carriage return | |||
\t Horizontal tabulator | |||
\v Vertical tabulator | |||
</pre> | |||
== Dates & Times == | |||
<pre> | |||
d = new Date(); | |||
var year = d.getFullYear(); | |||
var month = d.getMonth() + 1; | |||
</pre> | |||
<pre> | |||
function ISO8601_week_no() | |||
{ | |||
dt = new Date(); | |||
var tdt = new Date(dt.valueOf()); | |||
var dayn = (dt.getDay() + 6) % 7; | |||
tdt.setDate(tdt.getDate() - dayn + 3); | |||
var firstThursday = tdt.valueOf(); | |||
tdt.setMonth(0, 1); | |||
if (tdt.getDay() !== 4) | |||
{ | |||
tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7); | |||
} | |||
return 1 + Math.ceil((firstThursday - tdt) / 604800000); | |||
} | |||
</pre> | |||
= Node-Red = | |||
== Global variables == | |||
<pre> | |||
var labelmode = global.get("labelmode") | |||
</pre> | |||
== Node Status == | |||
<pre> | |||
node.status({fill:"gray",shape:"dot",text:msg.serialnumber + " = " + msg.pclass + "W"}); | |||
</pre> | |||
== Stop messages == | |||
<pre> | |||
if ( ...)) { | |||
return null; | |||
} else { | |||
return msg; | |||
} | |||
</pre> | |||
== InfluxDB == | |||
* write data with custom timestamp | |||
<pre> | |||
var timestamp = new Date(date + 'Z').getTime() * 1e6 | |||
msg = { payload : { value: value, | |||
time: timestamp }, | |||
measurement: topic } | |||
</pre> | </pre> |
Latest revision as of 11:20, 10 February 2024
Data Types
var num = 8; // number var strg = "Joe"; // string var vobj = {first: "John", last:"Doe"}; // object var booli = false; // boolean var arr = ["HTML","CSS","JS"]; // array var a; typeof a; // undefined var a = null; // value null const pi = 3.14; typeof strg // =string
Arrays
var title = new Array(); title[0] = "Mr."; title[1] = "Mrs."; element = title.slice(-1);
concat() filter() forEach() join() map() pop() push() reduce() reverse() shift() slice() splice() sort()
Strings
charAt() charCodeAt() concat() fromCharCode() indexOf() lastIndexOf() match() replace() zpl = zpl.replace("1234567890",msg.serialnumber) search() slice() checksum = String(sum).slice(-1) # get last digit split() substr() substring() toLowerCase() toUpperCase() valueOf() trim() remove whitespace from both sides trimStart() trimEnd() includes() if (string.includes(".prn")) {}
- Remove ANSI colors from string
payload = payload.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
- add leading zeros
var string = "000000" + integer.toString() string = string.substr(string.length-6)
Conversions
string = integer.toString() integer = parseInt(string) + 1
Functions
function addition(number1, number2) { return number1 + number2; } var person = { name: 'Peter', getName: function () { return this.name; } } person.getName()
Operators
+ Addition - Subtraction * Multiplication / Division (...) Grouping operator, operations within brackets are executed earlier than those outside % Modulus (remainder ) ++ Increment numbers -- Decrement numbers == Equal to === Equal value and equal type != Not equal !== Not equal value or not equal type > Greater than < Less than >= Greater than or equal to <= Less than or equal to ? Ternary operator && Logical and || Logical or ! Logical not & AND statement | OR statement ~ NOT ^ XOR << Left shift >> Right shift >>> Zero fill right shift
if - else
if (condition) { // if condition is met } else { // if condition is not met }
switch
switch(var) { case 1: // code break; case 2: // code break; default: // code }
Loops
for (var i = 0; i < 10; i++){ } while (i < 10) { i++; } do { i++; } while (i < 10); break continue
Escape Characters
\' Single quote \" Double quote \\ Backslash \b Backspace \f Form feed \n New line \r Carriage return \t Horizontal tabulator \v Vertical tabulator
Dates & Times
d = new Date(); var year = d.getFullYear(); var month = d.getMonth() + 1;
function ISO8601_week_no() { dt = new Date(); var tdt = new Date(dt.valueOf()); var dayn = (dt.getDay() + 6) % 7; tdt.setDate(tdt.getDate() - dayn + 3); var firstThursday = tdt.valueOf(); tdt.setMonth(0, 1); if (tdt.getDay() !== 4) { tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7); } return 1 + Math.ceil((firstThursday - tdt) / 604800000); }
Node-Red
Global variables
var labelmode = global.get("labelmode")
Node Status
node.status({fill:"gray",shape:"dot",text:msg.serialnumber + " = " + msg.pclass + "W"});
Stop messages
if ( ...)) { return null; } else { return msg; }
InfluxDB
- write data with custom timestamp
var timestamp = new Date(date + 'Z').getTime() * 1e6 msg = { payload : { value: value, time: timestamp }, measurement: topic }