JavaScript 中,array
並不是 primitive data type,當我們有需要將用途或性質相近的數據存儲在一起, array
即可派上用場
let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
具有以下核心特徵
可調整大小的,並且可以包含不同資料類型的混合
let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
// array的值被稱為元素(element),目前有5元素,長度5
let anotherArray = [null, false, "Darren", 3.14, undefined];
元素必須使用非負整數作為 index 來訪問(= string index)
let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
console.log(friends[0]); // -> Grace
第一個元素在 index 0 處,第二個在 index 1 處,依此類推。最後一個元素在 Array 的長度減 1 處
會複製 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']
length
let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
console.log(friends.length);
push(element)
- 將一個或多個元素添加到陣列的末尾,並return陣列的新長度
let friends = ["Grace", "Mike", "Darren", "Amanda", "Candy"];
friends.push("Jared");
console.log(friends); // -> ['Grace', 'Mike', 'Darren', 'Amanda', 'Candy', 'Jared']
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
區分之的原因
copy by value
let deposit = 500;
let another = deposit;
another = 600; // -> deposit 500, another 600
copy by 引用 reference
let friends = ["a", "b", "c"];
let another = friends;
another[0] = "d"; // -> friends ["d", "b", "c"], another ["d", "b", "c"]
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
下一篇文章來學習函數的時間複雜度。