8 个有用的 JS 技巧

2021-04-29 13:29

阅读:581

标签:类型   不能   对象属性   red   基本数据类型   基本   条件   创建   关注   

1. 确保数组值

使用 grid ,需要重新创建原始数据,并且每行的列长度可能不匹配, 为了确保不匹配行之间的长度相等,可以使用Array.fill方法。

let array = Array(5).fill(‘‘);
console.log(array); // outputs (5) ["", "", "", "", ""]

2. 获取数组唯一值

 

ES6 提供了从数组中提取惟一值的两种非常简洁的方法。不幸的是,它们不能很好地处理非基本类型的数组。在本文中,主要关注基本数据类型。

 1 const cars = [
 2     ‘Mazda‘, 
 3     ‘Ford‘, 
 4     ‘Renault‘, 
 5     ‘Opel‘, 
 6     ‘Mazda‘
 7 ]
 8 const uniqueWithArrayFrom = Array.from(new Set(cars));
 9 console.log(uniqueWithArrayFrom); // outputs ["Mazda", "Ford", "Renault", "Opel"]
10 
11 const uniqueWithSpreadOperator = [...new Set(cars)];
12 console.log(uniqueWithSpreadOperator);// outputs ["Mazda", "Ford", "Renault", "Opel"]

3.使用展开运算符合并对象和对象数组

 

对象合并是很常见的事情,我们可以使用新的ES6特性来更好,更简洁的处理合并的过程。

 1 // merging objects
 2 const product = { name: ‘Milk‘, packaging: ‘Plastic‘, price: ‘5$‘ }
 3 const manufacturer = { name: ‘Company Name‘, address: ‘The Company Address‘ }
 4 
 5 const productManufacturer = { ...product, ...manufacturer };
 6 console.log(productManufacturer); 
 7 // outputs { name: "Company Name", packaging: "Plastic", price: "5$", address: "The Company Address" }
 8 
 9 // merging an array of objects into one
10 const cities = [
11     { name: ‘Paris‘, visited: ‘no‘ },
12     { name: ‘Lyon‘, visited: ‘no‘ },
13     { name: ‘Marseille‘, visited: ‘yes‘ },
14     { name: ‘Rome‘, visited: ‘yes‘ },
15     { name: ‘Milan‘, visited: ‘no‘ },
16     { name: ‘Palermo‘, visited: ‘yes‘ },
17     { name: ‘Genoa‘, visited: ‘yes‘ },
18     { name: ‘Berlin‘, visited: ‘no‘ },
19     { name: ‘Hamburg‘, visited: ‘yes‘ },
20     { name: ‘New York‘, visited: ‘yes‘ }
21 ];
22 
23 const result = cities.reduce((accumulator, item) => {
24   return {
25     ...accumulator,
26     [item.name]: item.visited
27   }
28 }, {});
29 
30 console.log(result);
31 /* outputs
32 Berlin: "no"
33 Genoa: "yes"
34 Hamburg: "yes"
35 Lyon: "no"
36 Marseille: "yes"
37 Milan: "no"
38 New York: "yes"
39 Palermo: "yes"
40 Paris: "no"
41 Rome: "yes"
42 */

4. 数组 map 的方法 (不使用Array.Map)

 

另一种数组 map 的实现的方式,不用 Array.map

Array.from 还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。如下:

 1 const cities = [
 2     { name: ‘Paris‘, visited: ‘no‘ },
 3     { name: ‘Lyon‘, visited: ‘no‘ },
 4     { name: ‘Marseille‘, visited: ‘yes‘ },
 5     { name: ‘Rome‘, visited: ‘yes‘ },
 6     { name: ‘Milan‘, visited: ‘no‘ },
 7     { name: ‘Palermo‘, visited: ‘yes‘ },
 8     { name: ‘Genoa‘, visited: ‘yes‘ },
 9     { name: ‘Berlin‘, visited: ‘no‘ },
10     { name: ‘Hamburg‘, visited: ‘yes‘ },
11     { name: ‘New York‘, visited: ‘yes‘ }
12 ];
13 
14 const cityNames = Array.from(cities, ({ name}) => name);
15 console.log(cityNames);
16 // outputs ["Paris", "Lyon", "Marseille", "Rome", "Milan", "Palermo", "Genoa", "Berlin", "Hamburg", "New York"]

5. 有条件的对象属性

 

不再需要根据一个条件创建两个不同的对象,可以使用展开运算符号来处理。

nst getUser = (emailIncluded) => {
  return {
    name: ‘John‘,
    surname: ‘Doe‘,
    ...emailIncluded && { email : ‘john@doe.com‘ }
  }
}

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. 解构原始数据

 

有时候一个对象包含很多属性,而我们只需要其中的几个,这里可以使用解构方式来提取我们需要的属性。如一个用户对象内容如下:

const rawUser = {
   name: ‘John‘,
   surname: ‘Doe‘,
   email: ‘john@doe.com‘,
   displayName: ‘SuperCoolJohn‘,
   joined: ‘2016-05-05‘,
   image: ‘path-to-the-image‘,
   followers: 45
   ...
}

我们需要提取出两个部分,分别是用户及用户信息,这时可以这样做:

1 let user = {}, userDetails = {};
2 ({ name: user.name, surname: user.surname, ...userDetails } = rawUser);
3 
4 console.log(user); // outputs { name: "John", surname: "Doe" }
5 console.log(userDetails); // outputs { email: "john@doe.com", displayName: "SuperCoolJohn", joined: "2016-05-05", image: "path-to-the-image", followers: 45 }

7. 动态属性名

 

早期,如果属性名需要是动态的,我们首先必须声明一个对象,然后分配一个属性。这些日子已经过去了,有了ES6特性,我们可以做到这一点。

1 const dynamic = ‘email‘;
2 let user = {
3     name: ‘John‘,
4     [dynamic]: ‘john@doe.com‘
5 }
6 console.log(user); // outputs { name: "John", email: "john@doe.com" }

8.字符串插值

 

在用例中,如果正在构建一个基于模板的helper组件,那么这一点就会非常突出,它使动态模板连接容易得多。

 1 const user = {
 2   name: ‘John‘,
 3   surname: ‘Doe‘,
 4   details: {
 5     email: ‘john@doe.com‘,
 6     displayName: ‘SuperCoolJohn‘,
 7     joined: ‘2016-05-05‘,
 8     image: ‘path-to-the-image‘,
 9     followers: 45
10   }
11 }
12 
13 const printUserInfo = (user) => { 
14   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.`
15   console.log(text);
16 }
17 
18 printUserInfo(user);
19 // outputs ‘The user is John Doe. Email: john@doe.com. Display Name: SuperCoolJohn. John has 45 followers.‘

 


8 个有用的 JS 技巧

标签:类型   不能   对象属性   red   基本数据类型   基本   条件   创建   关注   

原文地址:https://www.cnblogs.com/Object-L/p/12200775.html


评论


亲,登录后才可以留言!