js skills

Replace All

 var example = "potato potato";

  the console.log (example.replace ( / POT /,  "Tom")); // replace a

  the console.log (example.replace ( / POT / G,  "Tom")); // global replacement

 

Extraction of unique value

entries It = var [ . 1,  2,  2,  . 3,  . 4,  . 5,  . 6,  . 6,  . 7,  . 7,  . 8,  . 4,  2,  . 1]
  var unique_entries = [new new ...  the Set (entries It)]; // Create a the new array with unique values

 

Converts a string to a digital

  the_string = "123";
console.log(+the_string);// 123

the_string = "hello";
console.log(+the_string); //NAN

 

Randomly arranged elements of the array

var my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(my_list.sort(function() {
   return Math.random() - 0.5
}));

 

Flatten multidimensional arrays

var entries = [1, [2, 5], [6, 7], 9];
var flat_entries = [].concat(...entries);

Shorten the conditional statement

IF (Available) {
    the addToCart ();
} can be replaced with

available && addToCart()

Dynamic property name

const dynamic = 'flavour';
var item = {
    name: 'Coke',
    [dynamic]: 'Cherry'
}
console.log(item); // { name: "Coke", flavour: "Cherry" }

Use length adjustment / empty array

var entries = [1, 2, 3, 4, 5, 6, 7];  
entries.length = 4;  
console.log(entries); // [1, 2, 3, 4]

Guess you like

Origin www.cnblogs.com/moneyss/p/12024368.html