我们依然可以使用ES6扩展运算符(...
)缩短这段长代码,如下所示:
let apples = [‘🍎’, ‘🍏’];
let fruits = […apples, ‘🥭’, ‘🍌’, ‘🍒’]; // <-- here
console.log( fruits );
//=> [“🍎”, “🍏”, “🥭”, “🍌”, “🍒”]
普通写法:
我们可以使用Array
中的slice()
方法轻松克隆数组,如下所示:
let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];
let cloneFruits = fruits.slice();
console.log( cloneFruits );
//=> [“🍉”, “🍊”, “🍇”, “🍎”]
简写方法:
我们可以使用ES6扩展运算符(...
)像这样克隆一个数组:
let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];
let cloneFruits = […fruits]; // <-- here
console.log( cloneFruits );
//=> [“🍉”, “🍊”, “🍇”, “🍎”]
普通写法:
在处理数组时,我们有时需要将数组“解包”成一堆变量,如下所示:
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 ); //=> 🍏
普通写法:
通常,当我们必须向字符串添加表达式时,我们会这样做:
// 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!
// Add two numbers
let num1 = 20;
let num2 = 10;
console.log(Sum = ${num1 + num2} and Subtract = ${num1 - num2}
);
//=> Sum = 30 and Subtract = 10
普通写法:
我们可以使用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
}
//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎
简写方法:
我们可以使用for...of
语句实现相同的结果,而代码要少得多,如下所示:
let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];
// Using for…of statement
for (let fruit of fruits) {
console.log( fruit );
}
//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎
普通写法:
要遍历数组,我们还可以使用Array
中的forEach()
方法。但是需要写很多代码,虽然比最常见的for
循环要少,但仍然比for...of
语句多一点:
let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];
// Using forEach method
fruits.forEach(function(fruit){
console.log( fruit );
});
//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎
简写方法:
但是使用箭头函数表达式,允许我们用一行编写完整的循环代码,如下所示:
let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];
fruits.forEach(fruit => console.log( fruit )); // <-- Magic ✨
//=> 🍉
//=> 🍊
//=> 🍇
//=> 🍎
大多数时候我使用的是带箭头函数的forEach
循环,这里我把for...of
语句和forEach
循环都展示出来,方便大家根据自己的喜好使用代码。
普通写法:
要通过其中一个属性从对象数组中查找对象的话,我们通常使用for
循环:
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 }
简写方法:
哇!上面我们写了这么多代码来实现这个逻辑。但是使用Array
中的find()
方法和箭头函数=>
,允许我们像这样一行搞定:
// 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 }
普通写法:
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
普通写法:
如果我们必须根据另一个值来设置一个值不是falsy值,一般会使用if-else
语句,就像这样:
function getUserRole(role) {
let userRole;
// If role is not falsy value
// set userRole
as passed role
value
if (role) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
最后
由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容,详细完整版的JavaScript面试题文档,或更多前端资料可以点此处免费获取。
朋友,同时减轻大家的负担。**
[外链图片转存中…(img-hQoy2SQN-1712072666224)]
[外链图片转存中…(img-fYIzCjpC-1712072666225)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
[外链图片转存中…(img-Nbj3RMqP-1712072666225)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
最后
[外链图片转存中…(img-sytr771s-1712072666226)]
[外链图片转存中…(img-ovSCEZ8p-1712072666226)]
由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容,详细完整版的JavaScript面试题文档,或更多前端资料可以点此处免费获取。