
分享下前端开发TIPS:8个实用的JavaScript应用代码,拿走不谢。
每种编程语言都有其独特的技巧。他们中的许多人是开发人员所熟知的,但是其中一些却有些骇人听闻。在本文中,我将向您展示一些我认为有用的技巧。我在实践中使用过的其中一些是解决旧问题的新方法。请享用!
1.确保数组值
是否曾经在需要重新创建原始数据的网格上工作,而每行的列长度可能不匹配?好吧,我有!为了确保不匹配的行之间的长度相等,可以使用Array.fill方法。
let array = Array(5).fill('');
console.log(array); // outputs (5) ["", "", "", "", ""]2.获取数组唯一值
ES6提供了两种非常整洁的方法来从数组中提取唯一值。不幸的是,它们不能很好地填充非基本类型的数组。您可以稍后在此链接上阅读有关此内容的更多信息。处理数组重复可能很棘手。在本文中,我们将重点介绍原始数据类型。
const cars = [ 'Mazda', 'Ford', 'Renault', 'Opel', 'Mazda' ] const uniqueWithArrayFrom = Array.from(new Set(cars)); console.log(uniqueWithArrayFrom); // outputs ["Mazda", "Ford", "Renault", "Opel"] const uniqueWithSpreadOperator = [...new Set(cars)]; console.log(uniqueWithSpreadOperator);// outputs ["Mazda", "Ford", "Renault", "Opel"]
3.使用扩展运算符合并对象和对象数组
对象合并不是一件难事,过去您很有可能做到了,将来也有可能做到。不同之处在于,过去您是手动完成大部分工作的,但是现在和将来,您将使用新的ES6功能。
// merging objectsconst product = { name: 'Milk', packaging: 'Plastic', price: '5$' }const manufacturer = { name: 'Company Name', address: 'The Company Address' }const productManufacturer = { ...product, ...manufacturer };console.log(productManufacturer); // outputs { name: "Company Name", packaging: "Plastic", price: "5$", address: "The Company Address" }// merging an array of objects into oneconst cities = [
{ name: 'Paris', visited: 'no' },
{ name: 'Lyon', visited: 'no' },
{ name: 'Marseille', visited: 'yes' },
{ name: 'Rome', visited: 'yes' },
{ name: 'Milan', visited: 'no' },
{ name: 'Palermo', visited: 'yes' },
{ name: 'Genoa', visited: 'yes' },
{ name: 'Berlin', visited: 'no' },
{ name: 'Hamburg', visited: 'yes' },
{ name: 'New York', visited: 'yes' }];const result = cities.reduce((accumulator, item) => {
return {
...accumulator,
[item.name]: item.visited }}, {});console.log(result);/* outputs
Berlin: "no"
Genoa: "yes"
Hamburg: "yes"
Lyon: "no"
Marseille: "yes"
Milan: "no"
New York: "yes"
Palermo: "yes"
Paris: "no"
Rome: "yes"
*/4.映射数组(不带Array.map)
您是否知道还有另一种不包含该Array.map方法的映射数组值的方法?如果没有,请在下面检查。
const cities = [
{ name: 'Paris', visited: 'no' },
{ name: 'Lyon', visited: 'no' },
{ name: 'Marseille', visited: 'yes' },
{ name: 'Rome', visited: 'yes' },
{ name: 'Milan', visited: 'no' },
{ name: 'Palermo', visited: 'yes' },
{ name: 'Genoa', visited: 'yes' },
{ name: 'Berlin', visited: 'no' },
{ name: 'Hamburg', visited: 'yes' },
{ name: 'New York', visited: 'yes' }
];
const cityNames = Array.from(cities, ({ name}) => name);
console.log(cityNames);
// outputs ["Paris", "Lyon", "Marseille", "Rome", "Milan", "Palermo", "Genoa", "Berlin", "Hamburg", "New York"]5.条件对象属性
不再需要根据条件创建两个不同的对象以使其具有特定的属性。为此,散布算子非常适合。
const getUser = (emailIncluded) => {
return {
name: 'John',
surname: 'Doe',
...emailIncluded && { email : 'john@doe.com' }
// or, if the && operator pattern is hard to read use ternary operator
// ...emailIncluded ? { email : 'john@doe.com' } : null
}
}
const user = getUser(true);
console.log(user); // outputs { name: "John", surname: "Doe", email: "john@doe.com" }
const userWithoutEmail = getUser(false);
console.log(userWithoutEmail); // outputs { name: "John", surname: "Doe" }6.破坏原始数据
您是否曾经处理过包含太多数据的对象?我很确定你有。可能最常见的情况是,当我们有一个包含整体数据以及详细信息的用户对象时。在这里,我们可以将新的ES破坏功能称为救援。让我们用一个示例对此进行备份。
const rawUser = {
name: 'John',
surname: 'Doe',
email: 'john@doe.com',
displayName: 'SuperCoolJohn',
joined: '2020-05-05',
image: 'path-to-the-image',
followers: 45
...
}可以通过拆分为两个对象,以更上下文的方式表示上面的对象,如下所示:
let user = {}, userDetails = {};
({ name: user.name, surname: user.surname, ...userDetails } = rawUser);
console.log(user); // outputs { name: "John", surname: "Doe" }
console.log(userDetails); // outputs { email: "john@doe.com", displayName: "SuperCoolJohn", joined: "2016-05-05", image: "path-to-the-image", followers: 45 }7.动态属性名称
在过去,我们首先必须声明一个对象,然后再分配一个属性(如果该属性名称需要动态)。这不可能以声明的方式实现。这些天已经过去,有了ES6功能,我们可以做到这一点。
const dynamic = 'email';
let user = {
name: 'John',
[dynamic]: 'john@doe.com'
}
console.log(user); // outputs { name: "John", email: "john@doe.com" }8.字符串插值
最后但并非最不重要的是串联字符串的新方法。如果您要构建基于模板的帮助程序组件,那么可以真正发挥作用的用例。它使动态模板串联变得容易得多。
const user = {
name: 'John',
surname: 'Doe',
details: {
email: 'john@doe.com',
displayName: 'SuperCoolJohn',
joined: '2016-05-05',
image: 'path-to-the-image',
followers: 45
}
}
const printUserInfo = (user) => {
const text = `The user is ${user.name} ${user.surname}. Email: ${user.details.email}. Display Name: ${user.details.displayName}. ${user.name} has ${user.details.followers} followers.`
console.log(text);
}
printUserInfo(user);
// outputs 'The user is John Doe. Email: john@doe.com. Display Name: SuperCoolJohn. John has 45 followers.'结论
JavaScript世界正在迅速扩展。这里有很多很酷的功能,随时可以使用。棘手且耗时的问题逐渐淡出了过去,并且借助ES6的新功能,可以立即使用解决方案。




网友评论文明上网理性发言 已有0人参与
发表评论: