您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關(guān)JS技巧有哪些,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
1. 確保數(shù)組值
使用 grid ,需要重新創(chuàng)建原始數(shù)據(jù),并且每行的列長度可能不匹配, 為了確保不匹配行之間的長度相等,可以使用Array.fill方法。
let array = Array(5).fill(''); console.log(array); // outputs (5) ["", "", "", "", ""]
2. 獲取數(shù)組唯一值
ES6 提供了從數(shù)組中提取惟一值的兩種非常簡潔的方法。不幸的是,它們不能很好地處理非基本類型的數(shù)組。在本文中,主要關(guān)注基本數(shù)據(jù)類型。
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.使用展開運算符合并對象和對象數(shù)組
對象合并是很常見的事情,我們可以使用新的ES6特性來更好,更簡潔的處理合并的過程。
// 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. 數(shù)組 map 的方法 (不使用Array.Map)
另一種數(shù)組 map 的實現(xiàn)的方式,不用 Array.map。
Array.from 還可以接受第二個參數(shù),作用類似于數(shù)組的map方法,用來對每個元素進行處理,將處理后的值放入返回的數(shù)組。如下:
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. 有條件的對象屬性
不再需要根據(jù)一個條件創(chuàng)建兩個不同的對象,可以使用展開運算符號來處理。
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. 解構(gòu)原始數(shù)據(jù)
有時候一個對象包含很多屬性,而我們只需要其中的幾個,這里可以使用解構(gòu)方式來提取我們需要的屬性。如一個用戶對象內(nèi)容如下:
const rawUser = { name: 'John', surname: 'Doe', email: 'john@doe.com', displayName: 'SuperCoolJohn', joined: '2016-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. 動態(tài)屬性名
早期,如果屬性名需要是動態(tài)的,我們首先必須聲明一個對象,然后分配一個屬性。這些日子已經(jīng)過去了,有了ES6特性,我們可以做到這一點。
const dynamic = 'email'; let user = { name: 'John', [dynamic]: 'john@doe.com' } console.log(user); // outputs { name: "John", email: "john@doe.com" }
8.字符串插值
在用例中,如果正在構(gòu)建一個基于模板的helper組件,那么這一點就會非常突出,它使動態(tài)模板連接容易得多。
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.'
關(guān)于“JS技巧有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。