iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 3
0
自我挑戰組

JavaScript基礎30天系列 第 3

陣列與物件 Day03

為什麼我們需要陣列呢??
想必你一定很疑惑/images/emoticon/emoticon04.gif
那麼接下來請繼續往下看囉!!

我們舉一個簡單的例子
https://ithelp.ithome.com.tw/upload/images/20200918/20123039qaHG66CLzd.jpg


陣列寫法
我們可以用一個 中括號 來呈現
https://ithelp.ithome.com.tw/upload/images/20200918/201230391CmWtgvJ6p.jpg
1.
那假如要讀取陣列第一筆資料
要如何寫呢??

這裡要先說明
陣列的索引(index)是從 0 開始
所以當我們讀取第一筆資料
如下圖:
https://ithelp.ithome.com.tw/upload/images/20200918/20123039CWfiCf0dEn.jpg
https://ithelp.ithome.com.tw/upload/images/20200918/20123039Ncj3AjobqU.jpg
2.
陣列一開始為空陣列 如何新增值到裡面呢??
(利用push這個方法)
https://ithelp.ithome.com.tw/upload/images/20200918/20123039AivosGtgZT.jpg
https://ithelp.ithome.com.tw/upload/images/20200918/20123039NLbJuzSGeR.jpg
3.
將陣列第一筆資料 由10改成20
https://ithelp.ithome.com.tw/upload/images/20200918/20123039pPA42Wxjb9.jpg
4.
陣列長度
https://ithelp.ithome.com.tw/upload/images/20200918/20123039zVE1YRJWuH.jpg
因為有3筆資料 所以長度為3
https://ithelp.ithome.com.tw/upload/images/20200918/201230394D5Hxt3UoJ.jpg


物件介紹

若有一個農場
有農夫 小雞 狗 與 玉米田
我們宣告4個變數
https://ithelp.ithome.com.tw/upload/images/20200918/20123039cqfgk7eLHp.jpg

但我們有時候需要得知
一個特定農場所包含的東西
這時我們就可以用 物件

https://ithelp.ithome.com.tw/upload/images/20200918/20123039tAHKgUrIMM.jpg

  • 屬性與值中間記得用 :
  • 用, 來區隔

物件新增
https://ithelp.ithome.com.tw/upload/images/20200918/20123039igMZFhV95g.jpg
https://ithelp.ithome.com.tw/upload/images/20200918/20123039ZIAquQKLEv.jpg
2.
取得第一隻狗的名子
https://ithelp.ithome.com.tw/upload/images/20200918/20123039E4I2BmPqKF.jpg
https://ithelp.ithome.com.tw/upload/images/20200918/20123039SdxiWC92iE.jpg


物件與function

物件除了可以放
字串,數值,布林值,陣列
當然還可以放 function

https://ithelp.ithome.com.tw/upload/images/20200918/20123039bLlykmxopQ.jpg
注意:
1.
這裡的function
可以不用給函式名稱
2.
要記得執行函式


物件+陣列
https://ithelp.ithome.com.tw/upload/images/20200918/20123039VPjAivkAht.jpg
https://ithelp.ithome.com.tw/upload/images/20200918/20123039vOx7Eoesez.jpg

今天的陣列物件就告此一段落
明天請繼續關注唷!!/images/emoticon/emoticon08.gif


上一篇
函式(function)基礎介紹 Day02
下一篇
運算子 基礎介紹 Day04
系列文
JavaScript基礎30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言