Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. /** Object.entries method (ES2017)*/
  2. let obj = {one: 1, two: 2, three: 3};
  3. //convert from object to array (key-value)
  4. console.log(Object.entries(obj)); // =>[["one", 1], ["two", 2], ["three", 3]]
  5.  
  6. /** Object.fromEntries method (ES2019) */
  7.  
  8. //This is reverse method
  9. //first example
  10. const myArr = [['one', 1], ['two', 2], ['three', 3]];
  11. obj = Object.fromEntries(myArray);
  12. console.log(obj); // => {one: 1, two: 2, three: 3}
  13.  
  14. //second example
  15. const map = new Map();
  16. map.set('one', 1);
  17. map.set('two', 2);
  18. obj = Object.fromEntries(map);
  19. console.log(obj); // => {one: 1, two: 2}
  20.  
  21. //example with url's params
  22. const paramsString = 'param1=foo&param2=baz';
  23. const searchParams = new URLSearchParams(paramsString);
  24. Object.fromEntries(searchParams); // => {param1: "foo", param2: "baz"}
  25.  
  26.  
  27.  
  28. /** trimStart() and trimEnd() methods (ES2019) */
  29. const str = " string ";
  30. console.log(str.trimStart()); // => "string "
  31. console.log(str.trimEnd()); // => " string"
  32.  
  33. /** trimLeft() and trimRight() methods */
  34. console.log(str.trimLeft()); // => "string "
  35. console.log(str.trimRight()); // => " string"
  36.  
  37.  
  38.  
  39. /** flat() and flatMap() methods (ES2019) */
  40. /** flat() */
  41. let arr = ['a', 'b', ['c', 'd']];
  42. let flattened = arr.flat();
  43. console.log(flattened); // => ["a", "b", "c", "d"]
  44.  
  45. arr = ['a', , , 'b', ['c', 'd']];
  46. flattened = arr.flat();
  47. console.log(flattened); // => ["a", "b", "c", "d"]
  48.  
  49. /** flat() also accepts an optional argument that specifies the number of levels a nested array should be flattened.
  50. If no argument is provided, the default value of 1 will be used: */
  51. arr = [10, [20, [30]]];
  52. console.log(arr.flat()); // => [10, 20, [30]]
  53. console.log(arr.flat(1)); // => [10, 20, [30]]
  54. console.log(arr.flat(2)); // => [10, 20, 30]
  55.  
  56. /** flatMap() */
  57. const arr = [4.25, 19.99, 25.5];
  58. console.log(arr.map(value => [Math.round(value)])); // => [[4], [20], [26]]
  59. console.log(arr.flatMap(value => [Math.round(value)])); // => [4, 20, 26]
  60.  
  61. /** String.proptotype.matchAll (ES2020) */
  62. const re = /(Dr\. )\w+/g;
  63. const str = 'Dr. Smith and Dr. Anderson';
  64. const matches = str.matchAll(re);
  65. for (const match of matches) {
  66. console.log(match);
  67. }
  68.  
  69. // logs:
  70. // => ["Dr. Smith", "Dr. ", index: 0, input: "Dr. Smith and Dr. Anderson", groups: undefined]
  71. // => ["Dr. Anderson", "Dr. ", index: 14, input: "Dr. Smith and Dr. Anderson", groups: undefined]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement