陳靖武
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 陣列方法 資料如下: ```javascript= const people = [ { name:'卡斯伯', order:'鍋燒意麵', price: 80 }, { name:'小明', order:'牛肉麵', price: 120 }, { name: '漂亮阿姨', order: '滷味切盤', price: 40 }, { name:'Ray', order:'大麻醬乾麵', price:60 }, ]; ``` 陣列索引位置: ![](https://i.imgur.com/Ddow0ai.png) > forEach 是這幾個陣列函式最單純的一個,不會額外回傳值,只單純執行每個陣列內的物件或值。 forEach不能跳出迴圈,for才可以。 > 使用 map() 時他需要回傳一個值,他會透過函式內所回傳的值組合成一個陣列。 > filter() 會回傳一個陣列,其條件是 return 後方為 true 的物件,很適合用在搜尋符合條件的資料。 > findIndex() 方法將依據提供的測試函式,尋找陣列中符合的元素,並返回其 index(索引)。如果沒有符合的對象,將返回 -1 。 > reduce() 方法將一個累加器及陣列中每項元素(由左至右)傳入回呼函式,將陣列化為單一值。 > sort() 方法會原地(in place)對一個陣列的所有元素進行排序,並回傳此陣列。排序不一定是穩定的(stable)。預設的排序順序是根據字串的 Unicode 編碼位置(code points)而定。 參數包含三個:物件, 索引, 全部陣列,可以只寫物件和索引,全部陣列很少用到 ### 1. 一一列出每個人的訂單 ```javascript= people.forEach(function(obj,key){ console.log(obj,key); }); ``` ![](https://i.imgur.com/J424FMA.png) ### 2. 將所有訂單新增一個新價格,全部是80% #### 1.使用forEach ```javascript= const newOrders = []; people.forEach(function(obj, key){ newOrders[key] ={ //索引位置 ...obj, //展開 newPrice:obj.price *0.8 //新價格 } }); console.log(newOrders); ``` #### 2.使用map ```javascript= const newOrders = people.map(function(item){ return { ...item, newPrice:item.price * 0.8 } }); console.log(newOrders); ``` #### 3.map更進階寫法(箭頭函式) ```javascript= const newOrders = people.map(item => ({ ...item, newPrice: item.price * 0.8 })) console.log(newOrders); ``` ### 3. 80元以上可以給滷蛋 #### 1. 使用forEach ```javascript= const newOrders2 =[]; //沒有調整 people.forEach(function(item, index){ //參數可以自己命名 if(item.price>=80){ newOrder2.push(item) } }); ``` #### 2.使用filter ```javascript= const newOrder2 = people.filter(function(item, index){ return item.price >=80 //為真的,就會回傳物件 }); console.log(newOrder2); ``` #### 3.使用filter+箭頭函式 ```javascript= const newOrders2 = people.filter(item => item.price >=80); console.log(newOrders2); ``` ### 4. 老闆發現牛肉沒了,把點牛肉麵換成牛肉湯麵 #### 1.forEach ```javascript= let index= 0 //let的數值會被調整 people.forEach(function(obj,key){ if (obj.order ==='牛肉麵'){ index = key; } }); people[index].order = '牛肉湯麵' //用中括號尋找索引位置 console.log(people[index]); ``` #### 2.findIndex ```javascript= const index = people.findIndex(function(item){ return item.order ==='牛肉麵'; }) people[index].order = '牛肉湯麵'; console.log(people[index]); ``` #### 3.findIndex+arrow ```javascript= const index = people.findIndex(item => item.order ==='牛肉麵') people[index].order = '牛肉湯麵' console.log(people[index]); ``` ### 5. 老闆說POS機壞了,麻煩幫忙出一下LI結構,方便列印發票 #### 1.forEach ```javascript= let htmlTemplate = '' //樣板字面值 people.forEach(function(obj,key){ htmlTemplate = htmlTemplate +`<li>//累加 ${obj.order},${obj.price} //將變數丟進去 </li>`; }); console.log(htmlTemplate); ``` #### 2.map ```javascript= const htmlTemplate = people.map(function(obj,key){ retrun `<li> ${obj.order}, ${obj.price} </li>` }).join('');//陣列轉字串 console.log(htmlTemplate); ``` #### 3.map加箭頭函式 ```javascript= const htmlTemplate = people.map((obj,key) =>`<li> ${obj.order}, ${obj.price} </li>` ).join('');//陣列轉字串 console.log(htmlTemplate); ``` ### 6. 老闆要收錢了,請問老闆應該收多少錢 #### 1.forEach ```javascript= let total = 0; people.forEach(function(obj,key){ total += obj.price; }) console.log(total); ``` #### 2.reduce 陣列前後有相關的時候用reduce ```javascript= let total = people.reduce(function(acc,cur){ //acc之前的值,cur當前的值 return acc+cur.price; }, 0); console.log(total); ``` #### 3 reduce+箭頭 ```javascript= const total = people.reduce((acc,cur)=> acc+cur.price,0); console.log(total); ``` ### 7.今天誰吃最貴! 請排序所有金額 sort為兩兩比較的結果 ```javascript= const peopleSort = people.sort((a, b) =>{ //a,b兩兩比較 return a.price -b.price //ab顛倒則排序由大到小 }); console.log(peopleSort); ```

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully