找回密碼
 立即註冊
查看: 1916|回復: 7

JS : Array Destructuring

[複製鏈接]

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

發表於 2021-11-12 23:13:19 | 顯示全部樓層 |閱讀模式
原本寫法:
  1. const profile = ["Oluwatobi", "Sofela", "google.com"];

  2. const firstName = profile[0];
  3. const lastName = profile[1];
  4. const website = profile[2];

  5. console.log(firstName); // "Oluwatobi"
  6. console.log(lastName); // "Sofela"
  7. console.log(website); // "google.com"
複製代碼
可以寫成:
  1. const profile = ["Oluwatobi", "Sofela", "google.com"];

  2. const [firstName, lastName, website] = profile;

  3. console.log(firstName); // "Oluwatobi"
  4. console.log(lastName); // "Sofela"
  5. console.log(website); // "google.com"
複製代碼


[發帖際遇]: 一個袋子砸在了 admin 頭上,admin 賺了 1 金錢. 幸運榜 / 衰神榜

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2021-11-12 23:15:36 | 顯示全部樓層
Direct Array Destructuring
  1. const [firstName, lastName, website] = [
  2.     "Oluwatobi",
  3.     "Sofela",
  4.     "google.com"
  5. ];

  6. console.log(firstName); // "Oluwatobi"
  7. console.log(lastName); // "Sofela"
  8. console.log(website); // "google.com"
複製代碼


[發帖際遇]: 一個袋子砸在了 admin 頭上,admin 賺了 3 金錢. 幸運榜 / 衰神榜

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2021-11-12 23:18:02 | 顯示全部樓層
也可以:
  1. let firstName, lastName, website;

  2. [firstName, lastName, website] = ["Oluwatobi", "Sofela", "google.com"];

  3. console.log(firstName); // "Oluwatobi"
  4. console.log(lastName); // "Sofela"
  5. console.log(website); // "google.com"
複製代碼


[發帖際遇]: admin 樂于助人,獎勵 5 貢獻. 幸運榜 / 衰神榜

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2021-11-12 23:22:02 | 顯示全部樓層
How to Use Array Destructuring to Assign the Rest of an Array Literal to a Variable
  1. const [firstName, ...otherInfo] = ["Oluwatobi", "Sofela", "google.com"];

  2. console.log(firstName); // "Oluwatobi"
  3. console.log(otherInfo); // ["Sofela", "google.com"]
複製代碼


[發帖際遇]: admin 樂于助人,獎勵 2 貢獻. 幸運榜 / 衰神榜

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2021-11-12 23:24:17 | 顯示全部樓層
How to Use Array Destructuring to Extract Values at Any Index
  1. const [, , website] = ["Oluwatobi", "Sofela", "google.com"];

  2. console.log(website); // "google.com"
複製代碼


[發帖際遇]: 一個袋子砸在了 admin 頭上,admin 賺了 1 金錢. 幸運榜 / 衰神榜

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2021-11-12 23:27:19 | 顯示全部樓層
How Default Values Work in an Array Destructuring Assignment
  1. const [firstName = "Tobi", website = "Google"] = ["Oluwatobi"];

  2. console.log(firstName); // "Oluwatobi"
  3. console.log(website); // "Google"
複製代碼


[發帖際遇]: admin 樂于助人,獎勵 4 貢獻. 幸運榜 / 衰神榜

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2021-11-12 23:29:37 | 顯示全部樓層
Swap 互換
  1. let firstName = "Oluwatobi";
  2. let website = "Google";

  3. [firstName, website] = [website, firstName];

  4. console.log(firstName); // "Google"
  5. console.log(website); // "Oluwatobi"
複製代碼


[發帖際遇]: admin 發帖時在路邊撿到 2 金錢,偷偷放進了口袋. 幸運榜 / 衰神榜

934

主題

649

回帖

3萬

積分

管理員

論壇管理員

積分
39487

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2021-11-12 23:40:30 | 顯示全部樓層
How to Use Array Destructuring to Extract Values from an Array to a Function’s Parameters
  1. // Define an array with two items:
  2. const profile = ["Oluwatobi", "Sofela"];

  3. // Define a function with one destructuring array containing two parameters:
  4. function getUserBio([firstName, lastName]) {
  5.   return `My name is ${firstName} ${lastName}.`;
  6. }

  7. // Invoke getUserBio while passing the profile array as an argument:
  8. getUserBio(profile);

  9. // The invocation above will return:
  10. "My name is Oluwatobi Sofela."
複製代碼


[發帖際遇]: admin 發帖時在路邊撿到 5 金錢,偷偷放進了口袋. 幸運榜 / 衰神榜
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DD論壇 維護: Redd Design

GMT+8, 2024-9-8 08:39 , Processed in 0.064635 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表