中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

工作記錄:8個有用(yong)的(de)JS技巧

這里(li)給大家分享我最近學習到的(de)8個有(you)用(yong)的(de)js小技巧,廢話不多說,我們(men)上代碼

1. 確保數組值
  使用 grid ,需要重新創建原始數據,并且每行的列長度可能不匹配, 為了確保不匹配行之間的長度相等,可以使用Array.fill方法

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

2. 獲取數組唯一值  

  ES6 提供了從數(shu)組中(zhong)提取惟(wei)一值的兩(liang)種非常簡潔的方法。不(bu)幸的是(shi),它(ta)們不(bu)能很(hen)好(hao)地(di)處理非基本(ben)類型(xing)的數(shu)組。在(zai)本(ben)文(wen)中(zhong),主(zhu)要關注(zhu)基本(ben)數(shu)據類型(xing)。

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.使用展開運算符合并對象和對象數組
  對(dui)象合并是很常(chang)見的(de)事情,我們(men)可(ke)以使用新的(de)ES6特(te)性(xing)來(lai)更好,更簡潔(jie)的(de)處理合并的(de)過程。

// merging objects
const 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 one
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 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. 數組 map 的方法 (不使用Array.Map)
  另一種(zhong)數組(zu) map 的實現的方(fang)式,不用 Array.map。

Array.from 還可以接受第(di)二個參數,作用類似于數組的(de)(de)map方法,用來對每(mei)個元素進行處理(li),將處理(li)后的(de)(de)值放入返回(hui)的(de)(de)數組。如下:

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. 有條件的對象屬性
  不再需要根據一個(ge)條件創建兩個(ge)不同(tong)的對象,可以(yi)使(shi)用展開運算符號來處理(li)。

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. 解構原始數據
  有時候(hou)一個(ge)(ge)對(dui)(dui)象(xiang)包含很多屬性(xing),而我們(men)只需要其中的(de)(de)幾個(ge)(ge),這(zhe)里可以使用解構(gou)方式來提(ti)取我們(men)需要的(de)(de)屬性(xing)。如一個(ge)(ge)用戶對(dui)(dui)象(xiang)內容(rong)如下:

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

我們需要提取出兩個部分(fen),分(fen)別是用戶及用戶信(xin)息,這(zhe)時可以這(zhe)樣(yang)做:

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. 動態屬性名
  早期,如果屬性(xing)名需要是動態(tai)的,我們首先必(bi)須聲明一個(ge)對象(xiang),然(ran)后分配(pei)一個(ge)屬性(xing)。這(zhe)些(xie)日子已經過去了(le),有(you)了(le)ES6特性(xing),我們可(ke)以做到(dao)這(zhe)一點。

const dynamic = 'email';
let user = {
name: 'John',
[dynamic]: 'john@doe.com'
}
console.log(user); // outputs { name: "John", email: "john@doe.com" }

8.字符串插值
  在用例中,如果正在構建一個(ge)基于模板的helper組件,那么這一點(dian)就會非(fei)常突出,它使動態模板連接容(rong)易得多(duo)。

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.'

如果對您有所幫助,歡迎您點個關注,我會定時更新技術文檔,大家一起討論學習,一起進步。

 

 

 

posted @ 2021-08-29 20:27  林恒  閱讀(416)  評論(0)    收藏  舉報