-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_notes.txt
39 lines (27 loc) · 1.26 KB
/
js_notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Display/print a JavaScript object:
# https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object
console.log(JSON.stringify(obj, null, 4))
# Check if an object is empty:
# https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
Object.keys(obj).length === 0;
# Or use lodash:
_.isEmpty(obj)
# Copy array by value:
# https://stackoverflow.com/questions/7486085/copy-array-by-value
let newArray = oldArray.slice();
# Sort array of objects by date key:
# https://stackoverflow.com/questions/8837454/sort-array-of-objects-by-single-key-with-date-value
arr.sort((a,b) => new Date(a.updated_at).getTime() - new Date(b.updated_at).getTime());
# Get type of an object:
# https://stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable
function typeOf(obj) {
return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
}
# Use lodash in the nodejs REPL:
# https://github.com/jsforce/jsforce/issues/470
var l_ = require('lodash');
# Check for presence of a key/field in an object:
# https://stackoverflow.com/questions/35948669/how-to-check-if-a-value-exists-in-an-object-using-javascript
let exists = Object.keys(obj).includes("test1")
# For an array:
let exists = array.includes("test1")