iT邦幫忙

2023 iThome 鐵人賽

DAY 11
0
自我挑戰組

一個月的後端學習之旅系列 第 11

【DAY11】Array陣列、Reference Data Type 比較

  • 分享至 

  • xImage
  •  

Array 陣列

JavaScript 中,array 並不是 primitive data type,當我們有需要將用途或性質相近的數據存儲在一起, array 即可派上用場

let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];

具有以下核心特徵

  1. 可調整大小的,並且可以包含不同資料類型的混合

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    // array的值被稱為元素(element),目前有5元素,長度5
    
    let anotherArray = [null, false, "Darren", 3.14, undefined];
    
  2. 元素必須使用非負整數作為 index 來訪問(= string index)

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    
    console.log(friends[0]);  // -> Grace
    
  3. 第一個元素在 index 0 處,第二個在 index 1 處,依此類推。最後一個元素在 Array 的長度減 1 處

  4. 會複製 reference

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    friends[0] = "Michael";
    console.log(friends);  // -> ['Michael', 'Mike', 'Darren', 'Amanda', 'Candy']
    
    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    let anotherVariable = friends;
    anotherVariable[0] = "Michael";
    
    console.log("friends array變成是:");
    console.log(friends);
    // -> ['Michael', 'Mike', 'Darren', 'Amanda', 'Candy']
    //因為copy by reference的關係,friends的值也被更動
    
    console.log("anotherVariable array變成是:");
    console.log(anotherVariable);  // -> ['Michael', 'Mike', 'Darren', 'Amanda', 'Candy']
    

Array properties

length

let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
console.log(friends.length);

Array methods

  • push(element) - 將一個或多個元素添加到陣列的末尾,並return陣列的新長度

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    friends.push("Jared");
    console.log(friends);  // -> ['Grace', 'Mike', 'Darren', 'Amanda', 'Candy', 'Jared']
    

    array push

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    let returnValue = friends.push("Jared");
    console.log(returnValue);  // -> 6,return value 是陣列的新長度
    
  • pop() - 從陣列中刪除 最後一個元素並返回該元素

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    friends.pop();
    console.log(friends);  // -> ['Grace', 'Mike', 'Darren', 'Amanda']
    
  • shift() - 從陣列中刪除 第一個元素並返回刪除的元素

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    friends.shift();
    console.log(friends);  // -> ['Mike', 'Darren', 'Amanda', 'Candy']
    

    知道刪去誰

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    let myFriend = friends.shift();
    console.log(myFriend);  // -> Grace
    
  • unshift(element) - 將一個或多個元素添加到陣列的開頭,並return陣列的新長度

    let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
    friends.unshift("Wee");
    console.log(friends);  // -> ['Wee', 'Grace', 'Mike', 'Darren', 'Amanda', 'Candy']
    

當Array內部的元素還有Array時,就被稱為是array of arrays,這樣的語法是可行的

let arrs = ["Phoebe", ["fb","ig", [[], [], ["Darren"]]], false, []];

取值方法

let myArr = [
  ["name", "address", "age"],
  ["Phoebe", "Taiwan", 23],
  ["Darren", "America", 25],
];

console.log(myArr[1][0]);  // -> Phoebe

primitive data type V.S reference data type

區分之的原因

  • 通常 primitive data type 使用的 RAM 比較小,但 reference data type 很占 RAM
  • 用 copy by reference,當遇到物件導向程式設計 OOP 會事半功倍

primitive data type

  • copy by value

    let deposit = 500;
    let another = deposit;
    another = 600;  // -> deposit 500, another 600
    

reference data type

  • copy by 引用 reference

    let friends = ["a", "b", "c"];
    let another = friends;
    another[0] = "d";  // -> friends ["d", "b", "c"], another ["d", "b", "c"]
    

reference data type比較 需注意的事

let array1 = [1, 2, 3];
let array2 = [1, 2, 3];

console.log(array1 == array2);  // -> false,因為程式看的是RAM的位置
let array1 = [1, 2, 3];
let array2 = array1;  // assignment operation

console.log(array1 == array2);  // -> true

下一篇文章來學習函數的時間複雜度。


上一篇
【DAY10】認識函數、return
下一篇
【DAY12】函數的時間複雜度
系列文
一個月的後端學習之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言