Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
preface
The code may not be the best, but using these toolkits will make your development more productive with less effort. Check out 😘 to see if there is one you have used before
Time handling Momentjs
You can directly open the console on the official website to test the method
Time format processing
moment().format('YYYY-MM-DD HH:mm:ss')
Copy the code
- Time to get
- Time to calculate
For other functions, please refer to the “moment.js documentation”.
Numerals deal with numeraljs
You can directly open the console on the official website to test the method
- Thousandths, decimals, percent processing
Request path handling
Request path processing QS
- Go to the string stringify
qs.stringify({ a: 'b'.c: 'd'.e: 'f' });
// 'a=b&c=d&e=f'
qs.stringify({ a: 'b'.c: 'd'.e: 'f' }, { filter: ['a'.'e']});// 'a=b&e=f'
Copy the code
- Turn the object the parse
qs.parse('a=b&c=d&e=f');
// { a: 'b', c: 'd', e: 'f' }
Copy the code
Object
- EncodeURI (URI) handles special characters in path parameters
- decodeURI(encodeURI(URI))
Array processing
- Array.prototype.flat() flattens the Array inside the Array
Syntax depth: depth, default: 1
Var newArray = arr.flat([depth])
const arr1 = [[{name:'a'}, {name:'b'}],
[{name:'cc'}, {name:'bc'}]]
console.log(arr1.flat());
Copy the code
- Array.from() shallow copies an instance
Commonly used arrays are de-duplicated
function unique (arr) {
return Array.from(new Set(arr))
}
Copy the code
Object processing
When processing objects, pay attention to sorting if they are displayed in a list. The sorting of objects varies according to the Forin traversal rules, resulting in unstable order
Tree data processing
Say goodbye to tree branching tree, achieve infinite level multi – selection pull down
What other operations have you used to manipulate data? Comment on couch snatching