Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Object.entries method (ES2017)*/
- let obj = {one: 1, two: 2, three: 3};
- //convert from object to array (key-value)
- console.log(Object.entries(obj)); // =>[["one", 1], ["two", 2], ["three", 3]]
- /** Object.fromEntries method (ES2019) */
- //This is reverse method
- //first example
- const myArr = [['one', 1], ['two', 2], ['three', 3]];
- obj = Object.fromEntries(myArray);
- console.log(obj); // => {one: 1, two: 2, three: 3}
- //second example
- const map = new Map();
- map.set('one', 1);
- map.set('two', 2);
- obj = Object.fromEntries(map);
- console.log(obj); // => {one: 1, two: 2}
- //example with url's params
- const paramsString = 'param1=foo¶m2=baz';
- const searchParams = new URLSearchParams(paramsString);
- Object.fromEntries(searchParams); // => {param1: "foo", param2: "baz"}
- /** trimStart() and trimEnd() methods (ES2019) */
- const str = " string ";
- console.log(str.trimStart()); // => "string "
- console.log(str.trimEnd()); // => " string"
- /** trimLeft() and trimRight() methods */
- console.log(str.trimLeft()); // => "string "
- console.log(str.trimRight()); // => " string"
- /** flat() and flatMap() methods (ES2019) */
- /** flat() */
- let arr = ['a', 'b', ['c', 'd']];
- let flattened = arr.flat();
- console.log(flattened); // => ["a", "b", "c", "d"]
- arr = ['a', , , 'b', ['c', 'd']];
- flattened = arr.flat();
- console.log(flattened); // => ["a", "b", "c", "d"]
- /** flat() also accepts an optional argument that specifies the number of levels a nested array should be flattened.
- If no argument is provided, the default value of 1 will be used: */
- arr = [10, [20, [30]]];
- console.log(arr.flat()); // => [10, 20, [30]]
- console.log(arr.flat(1)); // => [10, 20, [30]]
- console.log(arr.flat(2)); // => [10, 20, 30]
- /** flatMap() */
- const arr = [4.25, 19.99, 25.5];
- console.log(arr.map(value => [Math.round(value)])); // => [[4], [20], [26]]
- console.log(arr.flatMap(value => [Math.round(value)])); // => [4, 20, 26]
- /** String.proptotype.matchAll (ES2020) */
- const re = /(Dr\. )\w+/g;
- const str = 'Dr. Smith and Dr. Anderson';
- const matches = str.matchAll(re);
- for (const match of matches) {
- console.log(match);
- }
- // logs:
- // => ["Dr. Smith", "Dr. ", index: 0, input: "Dr. Smith and Dr. Anderson", groups: undefined]
- // => ["Dr. Anderson", "Dr. ", index: 14, input: "Dr. Smith and Dr. Anderson", groups: undefined]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement