31,876

# 1.合并数组

``````let apples = ['🍎', '🍏'];
let fruits = ['🍉', '🍊', '🍇'].concat(apples);

console.log( fruits );
//=> ["🍉", "🍊", "🍇", "🍎", "🍏"]

``````

``````let apples = ['🍎', '🍏'];
let fruits = ['🍉', '🍊', '🍇', ...apples];  // <-- here

console.log( fruits );
//=> ["🍉", "🍊", "🍇", "🍎", "🍏"]

``````

# 2.合并数组（在开头位置）

``````let apples = ['🍎', '🍏'];
let fruits = ['🥭', '🍌', '🍒'];

// Add all items from apples onto fruits at start
Array.prototype.unshift.apply(fruits, apples)

console.log( fruits );
//=> ["🍎", "🍏", "🥭", "🍌", "🍒"]

``````

``````let apples = ['🍎', '🍏'];
let fruits = [...apples, '🥭', '🍌', '🍒'];  // <-- here

console.log( fruits );
//=> ["🍎", "🍏", "🥭", "🍌", "🍒"]

``````

# 3.克隆数组

``````let fruits = ['🍉', '🍊', '🍇', '🍎'];
let cloneFruits = fruits.slice();

console.log( cloneFruits );
//=> ["🍉", "🍊", "🍇", "🍎"]

``````

``````let fruits = ['🍉', '🍊', '🍇', '🍎'];
let cloneFruits = [...fruits];  // <-- here

console.log( cloneFruits );
//=> ["🍉", "🍊", "🍇", "🍎"]

``````

# 4.解构赋值

``````let apples = ['🍎', '🍏'];
let redApple = apples[0];
let greenApple = apples[1];

console.log( redApple );    //=> 🍎
console.log( greenApple );  //=> 🍏

``````

``````let apples = ['🍎', '🍏'];
let [redApple, greenApple] = apples;  // <-- here

console.log( redApple );    //=> 🍎
console.log( greenApple );  //=> 🍏

``````

# 5.模板字面量

``````// Display name in between two strings
let name = 'Palash';
console.log('Hello, ' + name + '!');
//=> Hello, Palash!

// Add & Subtract two numbers
let num1 = 20;
let num2 = 10;
console.log('Sum = ' + (num1 + num2) + ' and Subtract = ' + (num1 - num2));
//=> Sum = 30 and Subtract = 10

``````

``````// Display name in between two strings
let name = 'Palash';
console.log(`Hello, \${name}!`);  // <-- No need to use + var + anymore
//=> Hello, Palash!

let num1 = 20;
let num2 = 10;
console.log(`Sum = \${num1 + num2} and Subtract = \${num1 - num2}`);
//=> Sum = 30 and Subtract = 10

``````

# 6.For循环

``````let fruits = ['🍉', '🍊', '🍇', '🍎'];

// Loop through each fruit
for (let index = 0; index < fruits.length; index++) {
console.log( fruits[index] );  // <-- get the fruit at current index
}

//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎

``````

``````let fruits = ['🍉', '🍊', '🍇', '🍎'];

// Using for...of statement
for (let fruit of fruits) {
console.log( fruit );
}

//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎

``````

# 7.箭头函数

``````let fruits = ['🍉', '🍊', '🍇', '🍎'];

// Using forEach method
fruits.forEach(function(fruit){
console.log( fruit );
});

//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎

``````

``````let fruits = ['🍉', '🍊', '🍇', '🍎'];
fruits.forEach(fruit => console.log( fruit ));  // <-- Magic ✨

//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎

``````

# 8.在数组中查找对象

``````let inventory = [  {name: 'Bananas', quantity: 5},  {name: 'Apples', quantity: 10},  {name: 'Grapes', quantity: 2}];

// Get the object with the name `Apples` inside the array
function getApples(arr, value) {
for (let index = 0; index < arr.length; index++) {

// Check the value of this object property `name` is same as 'Apples'
if (arr[index].name === 'Apples') {  //=> 🍎

// A match was found, return this object
return arr[index];
}
}
}

let result = getApples(inventory);
console.log( result )
//=> { name: "Apples", quantity: 10 }

``````

``````// Get the object with the name `Apples` inside the array
function getApples(arr, value) {
return arr.find(obj => obj.name === 'Apples');  // <-- here
}

let result = getApples(inventory);
console.log( result )
//=> { name: "Apples", quantity: 10 }

``````

# 9.将字符串转换为整数

`parseInt()`函数用于解析字符串并返回整数：

``````let num = parseInt("10")

console.log( num )         //=> 10
console.log( typeof num )  //=> "number"

``````

``````let num = +"10";

console.log( num )           //=> 10
console.log( typeof num )    //=> "number"
console.log( +"10" === 10 )  //=> true

``````

# 10.短路求值

``````function getUserRole(role) {
let userRole;

// If role is not falsy value
// set `userRole` as passed `role` value
if (role) {
userRole = role;
} else {

// else set the `userRole` as USER
userRole = 'USER';
}

return userRole;
}

console.log( getUserRole() )         //=> "USER"

``````

``````function getUserRole(role) {
return role || 'USER';  // <-- here
}

console.log( getUserRole() )         //=> "USER"

``````

# 补充几点

``````let fruits = ['🍉', '🍊', '🍇', '🍎'];
fruits.forEach(console.log);

``````

``````// Get the object with the name `Apples` inside the array
const getApples = array => array.find(({ name }) => name === "Apples");

let result = getApples(inventory);
console.log(result);
//=> { name: "Apples", quantity: 10 }

``````

``````const getUserRole1 = (role = "USER") => role;
const getUserRole2 = role => role ?? "USER";
const getUserRole3 = role => role ? role : "USER";

``````

# 编码习惯

• 规范的代码可以促进团队合作。
• 规范的代码可以减少 Bug 处理。
• 规范的代码可以降低维护成本。
• 规范的代码有助于代码审查。
• 养成代码规范的习惯，有助于程序员自身的成长。