grayshine
    • 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
    • Invite by email
      Invitee

      This note has no invitees

    • 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
    • Note Insights New
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
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
  • Invite by email
    Invitee

    This note has no invitees

  • 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # <font class="h2">陣列與物件混合應用</font> ###### tags: `javascript` <style> .h2 { background: linear-gradient(135deg,#fff,#537479) ; color: #537479; display:block; padding: 6px 5px; border-radius: 4px; } .h3 { background: linear-gradient(180deg,#fff 50%,#c9d5d4) ; color: #537479; display:block; padding: 6px 5px; border-bottom: 3px solid #537479; } .h4 { color: #537479; font-weight:bold; font-size:1.1em; } </style> <br> :::info ### 陣列與物件使用時機 - 陣列使用時機:當資料需要使用大量且同性質的資料 - 物件使用時機:要描述一個東西對應的特徵和行為。 ::: | | 陣列 | 物件 | | -------- | ----------------------------------------- | ---------------------------------------------- | | 型式 | `[]` | `{}` | | 讀取資料 | 陣列名稱[第幾筆] | 物件名稱.屬性名稱<br>物件名稱[屬性名稱] | | 新增資料 |`.push()`往後寫入<br>`.unshift()`往前寫入 | 物件名稱.屬性名稱=值<br> 物件名稱[屬性名稱]=值 | | 刪除資料 |`.pop()`刪除最後一筆<br>`.shift()`刪除最前面一筆<br>`.splice()`刪除其中資料 |delete 物件名稱.屬性名稱<br>delete 物件名稱[屬性名稱]| <br> | | 物件包陣列 | 陣列包物件 | | --- | ------------------------- | ----------------------------- | |取資料| 物件名稱.陣列名稱[第幾筆] | 陣列名稱[第幾筆物件].屬性名稱 | <br> | | 物件包陣列包物件 | 陣列包物件包陣列 | | --- | -------------------------------------- | --------------------------------------------- | |取資料| 物件名稱.陣列名稱[第幾筆物件].屬性名稱 | 陣列名稱[第幾筆物件].屬性名稱[第幾筆陣列資料] | ```javascript //有兩個水果攤 //第一個水果攤,老闆Tom,香蕉有6個,蘋果有5個,還沒關店 //第二個水果攤,老闆叫John,香蕉有9個,蘋果有8個,還沒關店 let market = [ { bossName: "Tom"; bananaNum:6, appleNum:5, isClose:false } { bossName: "John"; bananaNum:9, appleNum:8, isClose:false } ] ``` ### <font class="h3">讀取資料</font> ```javascript let market = [ { bossName: "Tom"; bananaNum:6, appleNum:5, isClose:false } { bossName: "John"; bananaNum:9, appleNum:8, isClose:false } ] console.log(market[0].bassName);//"Tom" console.lot(market[1].appleNum);//8 ``` :::info :bulb:當要尋找陣列時使用`[]`,當要尋找物件裡面屬性使用`.` ::: <br> ### <font class="h3">物件搭配 if</font> ```javascript let tomStatus = { name:"Tom", age:"30", status:"" } tomStatus.degree = 36.5; if(tomStatus.degree>=37.5){ console.log("您不得進入"); tomStatus.status = "您不得進入" }else{ console.log("您可以進入"); tomStatus.status = "您可以進入" } console.log(tomStatus) ``` <br> ### <font class="h3">陣列物件搭配 if </font> ```javascript let peopleStatus = [ { name:"Tom", age:"30", status:"" } ] peopleStatus[0].degree = 36.5; if(peopleStaus[0].degree>=37.5){ peopleStatus[0].status ="您不得進入" console.log("您不得進入") }else{ peopleStatus[0].status ="您可以進入" console.log("您可以進入") } console.log(peopleStatus[0]) ``` <br> ### <font class="h4">範例:</font> **範例1.** 母親今年的狀態需要更新,還請協助她修改物件資料 流程一:母親多了一歲了,幫他累加一歲在age屬性對應的value上 流程二:母親多了一位小孩,幫她在sons屬性新增一位"John" 請新增一行Code,寫console.log來印出motherStatus ```javascriopt let motherStatus = [ { name:"mary"; age:31; sons:["Tom","Bob"] } ] ``` :::spoiler ```javascript let motherStatus =[ { name:"mary", age:31, sons:["Tom","Bob"] } ] motherStatus[0].age += 1; motherStatus[0].sons.push("John"); console.log(motherStatus) ``` 也可以運用變數 ```javascript let motherStatus = [ { name: "Mary", age: 31, sons: ["Tom", "Bob"] } ]; let mom = motherStatus[0];//運用變數 mom.age += 1; mom.sons.push("John"); console.log(mom) ``` ::: <br> **範例2.** 老闆想要幫公司兩人都加薪到40000元 請透過以下資訊修改,幫老闆做加薪的動作 ```javascript let salaryData = { company: 'circle center', bossName: 'casper', staff: [ { name: "Bob", salary: 32000 }, { name: "Jack", salary: 28000 } ] } // 第一位員工加薪邏輯 if(salaryData.staff[0].salary < 40000){ salaryData.staff[0].salary = 40000; } // 第二位員工加薪邏輯 if(salaryData.staff[1].salary < 40000){ salaryData.staff[1].salary += 40000 - salaryData.staff[1].salary; } ```

    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