TreeviewCopyright © aleen42 all right reserved, powered by aleen42
Arrays Back
1. Use []
- To use literal syntax for array creation.
- Eslint rules tags:
no-array-constructor
/**
* bad
*/
const items = new Array();
/**
* good
*/
const items = [];
2. Use push()
- To use Array#push instead of direct assignment to add items to an array.
const arr = [];
/**
* bad
*/
arr[arr.length] = 'aleen';
/**
* good
*/
arr.push('aleen');
3. Use spread operator ...
- To use array spreads to copy arrays.
/**
* bad
*/
const len = items.length;
const itemsCopy = [];
let i;
for (i = 0; i < len; i++) {
itemsCopy[i] = items[i];
}
/**
* good
*/
const itemsCopy = [...items];
- Notice that: spread operator is only supported by ECMAScript 6 (ES6). Browser Compatibility
4. Use from()
- To convert an array-like object to an array, use Array#from.
const foo = document.querySelectorAll('.foo');
const arr = Array.from(foo);