1. Delete the trailing elements of an array
One simple way is to change the length of the array:
Const arr = [one, One, one, one, one, ];
Truncanting
Arr. Length = 3;
Console. Log(arr); //=> [One, one, one]
Clearing
Arr. Length = 0;
Console. Log(arr); //=> []
Console. Log(arr[2]); //=> undefined
2. Using object destructuring to simulate named parameters
If you need to pass in a series of options as arguments into a function, you will most likely use the object to define the configuration (config).
DoSomething({ foo: ' Hello ', bar: ' hey! ') , baz: );
function dosomething(config) {
const foo = config. Foo !== undefined ? config. Foo : ' Hi ';
Const bar = config. Bar !== undefined ? config. Bar : ' yo! ' ;
Const baz = config. Baz !== undefined ? config. Baz : ;
// ...
}
But this is an older approach, which simulates named parameters in JavaScript.
In ES 2015, you can use object deconstruction directly:
function dosomething({ foo = ' Hi ', bar = ' yo! ') , baz = ) {
// ...
}
Making parameters optional is also simple:
function dosomething({ foo = ' Hi ', bar = ' yo! ') , baz = + } = {}) {
// ...
}
3. Using object deconstruction to manipulate arrays
You can use the syntax of an object's deconstruction to get the elements of an array:
Const csvfileline = ' 1997,john doe,us,[email protected],new York ';
Const { 2: country, 4: state } = csvfileline< c23>. split(', ');
4. Using range values in a Switch statement
The statement that satisfies the range value can be written like this:
function getwaterstate(tempincelsius) {
Let state ;
Switch (true) {
case (tempincelsius 0):
state = ' Solid ';
break;
case (tempincelsius > 0 && tempincelsius ):
state = ' Liquid ';
break;
default:
state = ' gas ';
}
return state ;
}
5. Await multiple Async functions
When using async/await, you can use Promise.all to await multiple async functions
Await Promise. All([anasynccall(), thisisalsoasync(), Onemore()])
6. Create a pure objects
You can create a 100% pure object that does not inherit any attributes from object or methods (such as constructor, ToString (), and so on)
Const pureobject = Object. Create(null);
Console. Log(pureobject); //=> {}
Console. Log(pureobject. constructor); //=> undefined
Console. Log(pureobject. ToString); //=> undefined
Console. Log(pureobject. hasOwnProperty); //=> undefined
7. Formatting JSON Code
Json.stringify can also format the output JSON object in addition to the ability to char an object
Const obj = {
Foo: { bar: [one, one, and] , Baz: { bing: true, boom: ' Hello ' } c42>}
};
The third parameter is the number of spaces used to
Beautify the JSON output.
JSON. Stringify(obj, null, 4);
= = "{
= "Foo": {
= "Bar": [
= 11,
= 22,
= 33,
= 44
=],
= = "Baz": {
= = "Bing": true,
= "Boom": "Hello"
= =}
= =}
=>} "
8. Removing duplicate elements from an array
By using the collection syntax and the Spread operation, it is easy to remove duplicate elements:
Const removeduplicateitems = arr = = [... new Set(arr)];
Removeduplicateitems([a ], ' foo ', a , ' foo' , True, true]);
= = [A, "foo", True]
9. Tiling multidimensional Arrays
To tile nested multidimensional arrays using the Spread operation:
Const arr = [One, [a ] , [] ;
Const Flatarr = []. Concat(... arr); //=> [One, One, one, one, one, and one]
However, the above method only applies to two-dimensional arrays, but with recursion, you can tile arrays of arbitrary dimensions:
function flattenarray(arr) {
Const flattened = []. Concat(... arr);
return flattened. Some(item = Array. IsArray(item)) ?
Flattenarray(flattened) : flattened;
}
Const arr = [One, [a ] , [[n], [[+]] []] ;
Const Flatarr = flattenarray(arr);
= = [11, 22, 33, 44, 55, 66, 77, 88, 99]
5-minute mastery of JavaScript tips (from the public, Front end development)