2-44钟静雯_day03
Day03作業(yè) 數(shù)據(jù)結(jié)構(gòu)
概述 基本類型和引用類型
基本類型:String Boolean Number Null Undefined Symbol Bigint。
引用類型:Object(存儲(chǔ)屬性和屬性值) Function。
區(qū)別:
(內(nèi)存存儲(chǔ))基本類型存儲(chǔ)在棧(Stack)里面;引用類型在棧上存儲(chǔ)對堆上數(shù)據(jù)的引用(指針),在堆(Heap)上存儲(chǔ)引用類型本身的數(shù)據(jù)。
(清理方式)基本類型函數(shù)和方法執(zhí)行完就清理內(nèi)存;引用類型采用垃圾回收機(jī)制。
對象 存儲(chǔ)屬性和屬性值
定義對象:
Object構(gòu)造器
對象字面量
數(shù)組 有序的值的列表
(與其他編程語言相比)區(qū)別:
數(shù)組的每個(gè)槽位可以存儲(chǔ)任意類型的數(shù)據(jù)。
動(dòng)態(tài)大小 —— 影響性能。
定義數(shù)組:(聲明)
數(shù)組構(gòu)造器
數(shù)組字面量
初始化:
解構(gòu)數(shù)組:將數(shù)組中的元素取出來賦值給變量。
數(shù)組的屬性和方法:
length屬性:數(shù)組的長度。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
console.log(avengers.length);
// 通過設(shè)置length屬性改變數(shù)組元素(不可逆轉(zhuǎn)的改變,無副本)
avengers.length = 1;
console.log(avengers);// [ ‘美國隊(duì)長’ ]
avengers.length = 5;
console.log(avengers);// <1 empty item> Undefined
// 不改變數(shù)組元素:將length設(shè)為只讀屬性get。
pop()方法:刪掉數(shù)組中最后一個(gè)元素。
// 聲明數(shù)組
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
// 數(shù)組維數(shù)縮小
console.log(avengers.pop());
console.log(avengers);
// delete運(yùn)算符:元素個(gè)數(shù)沒變,刪掉元素變?yōu)閁ndefined
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
delete avengers[0];
console.log(avengers);
pop()方法和delete運(yùn)算符的區(qū)別:pop數(shù)組元素個(gè)數(shù)減少,delete運(yùn)算符元素個(gè)數(shù)沒變。
push()方法:將新值添加到數(shù)組的末尾。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
avengers.push(‘蝙蝠俠’);
console.log(avengers);
shift()方法:刪除數(shù)組中的第一個(gè)元素。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
avengers.shift();
console.log(avengers);// [ ‘鋼鐵俠’, ‘雷神’, ‘綠巨人’ ]
console.log(avengers.shift());// 輸出刪除元素 鋼鐵俠
unshift()方法:將新值添加到數(shù)組的開頭。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
avengers.unshift(‘小超人’);
console.log(avengers);
console.log(avengers.unshift());
concat()方法:數(shù)組合并。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const heroes = [‘蝙蝠俠’,‘神奇女俠’,‘閃電俠’,‘水行俠’];
const oArray = avengers.concat(heroes);// 賦值給變量,變量是新生成的數(shù)組。
console.log(avengers);
console.log(oArray);// 新建數(shù)組,進(jìn)行合并。
// 擴(kuò)展運(yùn)算符:ES6新增。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const heroes = [‘蝙蝠俠’,‘神奇女俠’,‘閃電俠’,‘水行俠’];
const oArray = […avengers,…h(huán)eroes];// 元素扁平化
console.log(oArray);
join()方法:數(shù)組變成組合了數(shù)組所有元素的字符串。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a = avengers.join(&);// 變量
console.log(a);
slice()方法:從原始數(shù)組中切掉一片,從而創(chuàng)建一個(gè)子數(shù)組。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const b = avengers.slice(2,3); // 從第二個(gè)元素開始到第三個(gè)元素結(jié)束(不包含)
console.log(b);// [ ‘雷神’ ]
console.log(avengers);
reverse()方法:反轉(zhuǎn)數(shù)組中元素的次序(永久性改變)。
const d = [‘a(chǎn)’,‘b’,‘c’,‘d’];
const e = d.reverse();// 賦值給變量
console.log(e,d);// [ ‘d’, ‘c’, ‘b’, ‘a(chǎn)’ ] [ ‘d’, ‘c’, ‘b’, ‘a(chǎn)’ ]
indexOf():檢測數(shù)組中是否包含一個(gè)特定值,如果找到了,就返回該值在數(shù)組中第一次出現(xiàn)的索引號,否則,就返回-1。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a1 = avengers.indexOf(‘美國隊(duì)長1’);
console.log(a1);// 0
includes():檢測數(shù)組中是否包含特定值,如果找到了,就返回true,否則就返回false。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a2 = avengers.includes(‘美國隊(duì)長2’);
console.log(a2);// false
多維數(shù)組:
作用:坐標(biāo)系統(tǒng)(二維數(shù)組)以及復(fù)雜算法。
舉例:
const ma = [[1,2],[3,4]];
console.log(ma[0][0]);// 訪問元素
const summer = [‘Jun’,‘Jul’,‘Aug’];
const winter = [‘Dec’,‘Jan’,‘Feb’];
const nested = [summer,winter];
console.log(nested);// [ [ ‘Jun’, ‘Jul’, ‘Aug’ ], [ ‘Dec’, ‘Jan’, ‘Feb’ ] ]
// 擴(kuò)展運(yùn)算符扁平化為字符串
const summer = [‘Jun’,‘Jul’,‘Aug’];
const winter = [‘Dec’,‘Jan’,‘Feb’];
const flat = […summer,…winter];
console.log(flat);// [ ‘Jun’, ‘Jul’, ‘Aug’, ‘Dec’, ‘Jan’, ‘Feb’ ]
1
2
3
4
5
6
7
8
9
10
11
12
數(shù)組去重:(Set和數(shù)組的轉(zhuǎn)換)
const a = [1,2,12,1,2,3,4,5];// 循環(huán)讀取一個(gè)一個(gè)判斷
const b = new Set(a);
console.log(b);// Set { 1, 2, 12, 3, 4, 5 }
const c = […b];// 集合變數(shù)組
console.log?;
1
2
3
4
5
集合 唯一值的集合
定義集合:
構(gòu)造函數(shù),無字面量。
// 數(shù)字
const list = new Set();
list.add(1);
list.add(2).add(3).add(4).add(5);// 鏈?zhǔn)酱鎯?chǔ)
list.add(5);// 不能有重復(fù)值,直接忽略。
console.log(list);
// 初始化時(shí)用數(shù)組
const list = new Set([1,2,3,4,5]);
console.log(list);
// 字符
const c = new Set(‘Hello’);
console.log?;
// 鏈?zhǔn)讲僮?#xff0c;返回的一定是創(chuàng)建的本身
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
console.log(list4);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
屬性和方法:
size屬性:獲取集合中值的數(shù)目。(只讀屬性)
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
console.log(list4.size);
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
list4.size = 3;
console.log(list4);
1
2
3
4
5
6
has()方法:用于檢測一個(gè)值是否在集合中,返回true或者false。
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
console.log(list4.has(‘brown’));
1
2
delete()方法:從集合中刪除一個(gè)值。
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
list4.delete(‘the’);
console.log(list4);
1
2
3
clear()方法:刪掉集合中的所有值。
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
list4.clear();
console.log(list4);
1
2
3
Set和數(shù)組的轉(zhuǎn)換:數(shù)組去重也用到了。擴(kuò)展運(yùn)算符 Array.from() 方法
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
const oArray = […list4]; // 集合轉(zhuǎn)數(shù)組
console.log(oArray);
// 數(shù)組方法
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
const oArray = Array.from(list4);
console.log(oArray);
1
2
3
4
5
6
7
8
集合 唯一值的集合
當(dāng)對象添加到Set中時(shí),只要Set存在,它們就會(huì)一直存儲(chǔ)在Set中,即使對對象的原始引用被刪除了依然如此。用技術(shù)術(shù)語來說,就是對象阻止被垃圾回收,而這會(huì)導(dǎo)致內(nèi)存泄漏。
WeakSet 通過垃圾回收任何引用原始引用已經(jīng)被刪掉的“死對象”,從而可以避免這種情況。
WeakSet 只能添加非基本類型數(shù)據(jù),否則會(huì)拋出一個(gè)類型錯(cuò)誤(TypeError)。
// Set強(qiáng)集合 內(nèi)存泄漏
let array1 = [1, 2, 3];
let array2 = [3, 4, 5];
const strong = new Set().add(array1).add(array2); // 建集合,添加數(shù)組。
console.log(strong.has(array1));
array1 = null; // 刪除對原始對象的引用
array2 = null; // 原來數(shù)組設(shè)置為空
array3 = […strong][0]; // 1
array4 = […strong][1];
console.log(array3);
console.log(array4);// 數(shù)組還在
// WeakSet 避免內(nèi)存泄漏
let array1 = [1,2,3];
let array2 = [3,4,5];
const weak = new WeakSet().add(array1).add(array2);// 扁平化
console.log(weak);
array1 = null; // 垃圾回收
array2 = null;
const array3 = […weak][0];
const array4 = […weak][1];
// 拋出異常
console.log(array3);
console.log(array4);// 類型錯(cuò)誤,對象消失,不能引用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Map 存儲(chǔ)鍵值對列表
創(chuàng)建Map:
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals);// Map { 1 => ‘I’, 2 => ‘II’, 3 => ‘III’, 4 => ‘IV’, 5 => ‘V’ }
1
2
3
4
方法和屬性:
size屬性:獲取鍵和值的數(shù)量。(只讀屬性)
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals.size);// 5
1
2
3
4
get(key):通過鍵獲取值。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals.get(3));// III
1
2
3
4
has(key):檢測一個(gè)特定鍵是否在映射中。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals.has(5));// true
1
2
3
4
delete(key):從映射中刪除一個(gè)鍵值對。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
romanNumerals.delete(5);
console.log(romanNumerals);// Map { 1 => ‘I’, 2 => ‘II’, 3 => ‘III’, 4 => ‘IV’ }
1
2
3
4
5
clear():從映射中刪除所有鍵值對。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
romanNumerals.clear();
console.log(romanNumerals);
1
2
3
4
5
Map轉(zhuǎn)換為數(shù)組:
擴(kuò)展運(yùn)算符
Array.from() 方法
//map to array
const romanNumerals = new Map();
romanNumerals.set(1,‘I’).set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);// 鏈?zhǔn)?br /> const oArray1 = […romanNumerals];
const oArray2 = Array.from(romanNumerals);
console.log(oArray1);
console.log(oArray2);
// [ [ 1, ‘I’ ], [ 2, ‘II’ ], [ 3, ‘III’ ], [ 4, ‘IV’ ], [ 5, ‘V’ ] ]
// [ [ 1, ‘I’ ], [ 2, ‘II’ ], [ 3, ‘III’ ], [ 4, ‘IV’ ], [ 5, ‘V’ ] ]
1
2
3
4
5
6
7
8
9
WeakMap
鍵不能是基本數(shù)據(jù)類型的值,并且在對原始對象的引用被刪除時(shí),垃圾回收會(huì)自動(dòng)刪除所有死條目。
// 內(nèi)存泄漏
let array5 = [1, ‘I’];
let array6 = [2, ‘II’];
strong1 = new Map().set(array5).set(array6); // 創(chuàng)建映射,添加數(shù)組。
console.log(strong1.has(array5));
array5 = null;
array6 = null;
array7 = […strong1][0];
array8 = […strong1][1];
console.log(array7);// [ [ 1, ‘I’ ], undefined ]
console.log(array8);// [ [ 2, ‘II’ ], undefined ]
let array9 = [1, ‘I’];
let array10 = [2, ‘II’];
weak1 = new WeakMap().set(array9).set(array10); // 創(chuàng)建映射,添加數(shù)組。
console.log(weak1.has(array9));
array9 = null;
array10 = null;
array11 = […Weak1][0];
array12 = […Weak1][1];
console.log(array11);
console.log(array12);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
總結(jié)
數(shù)據(jù)類型
基礎(chǔ)類型:String Number Boolean Symbol undefined null。
引用類型:Object/Function。
對象
創(chuàng)建對象的兩種方式
構(gòu)造器
let oStudent = new Object();
1
對象字面量
let oStudent = {
name: ‘xaaaa’,
age: 21
};
oStudent.name // 引用屬性
1
2
3
4
5
Array
1.創(chuàng)建數(shù)組的兩種方式
構(gòu)造器
const oArray = new Array();
1
數(shù)組字面量
let person1 = {};
1
2.初始化
逐個(gè)賦值
const heroes = [];
heroes[0] = ‘蝙蝠俠’;
heroes[1] = ‘神奇女俠’;
1
2
3
數(shù)組字面量
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
1
3.刪除數(shù)組元素
delete avengers[0];
1
4.解構(gòu)數(shù)組
const [a,b,c] = [1,2,3];
console.log(a=${a},b=${b},c=${c});
1
2
5.數(shù)組的屬性和方法
length屬性:數(shù)組的長度。
console.log(avengers.length);
1
pop()方法:刪掉數(shù)組中最后一個(gè)元素。
console.log(avengers.pop());
1
push()方法:將新值添加到數(shù)組的末尾。
avengers.push(‘蝙蝠俠’);
1
shift()方法:刪除數(shù)組中的第一個(gè)元素。
avengers.shift();
1
unshift()方法:將新值添加到數(shù)組的開頭。
avengers.unshift(‘小超人’);
1
concat()方法:數(shù)組合并。
const oArray = avengers.concat(heroes);
// 擴(kuò)展運(yùn)算符
const oArray = […avengers,…h(huán)eroes];
1
2
3
join()方法:數(shù)組變成組合了數(shù)組所有元素的字符串。
const a = avengers.join(&);
1
slice()方法:從原始數(shù)組中切掉一片,從而創(chuàng)建一個(gè)子數(shù)組。
const b = avengers.slice(2,3);
1
splice()方法:從一個(gè)數(shù)組中刪除元素,然后將新元素插入在被刪除的元素的位置上。
const cc = avengers.splice(2,2,‘liwanling’,‘li’);
1
reverse()方法:反轉(zhuǎn)數(shù)組中元素的次序(永久性改變)。
const e = d.reverse();
1
sort()方法:對數(shù)組中的元素按字母順序進(jìn)行排序(永久性改變)。
const g = f.sort();
1
indexOf():檢測數(shù)組中是否包含一個(gè)特定值,如果找到了,就返回該值在數(shù)組中第一次出現(xiàn)的索引號,否則,就返回-1。
const avengers = [‘美國隊(duì)長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a1 = avengers.indexOf(‘美國隊(duì)長1’);
console.log(a1);// 0
1
2
3
includes():檢測數(shù)組中是否包含特定值,如果找到了,就返回true,否則就返回false。
const a2 = avengers.includes(‘美國隊(duì)長2’);
1
Set:沒有重復(fù)值
1.創(chuàng)建Set
構(gòu)造器
let oSet = new Set();
1
2.添加元素
oSet.add(1).add(2)
1
3.Set的屬性和方法
size屬性:獲取集合中值的數(shù)目。
console.log(list4.size);
1
has()方法:用于檢測一個(gè)值是否在集合中,該方法會(huì)返回true或者false。
console.log(list4.has(‘brown’));
1
delete()方法:從集合中刪除一個(gè)值。
list4.delete(‘the’);
1
clear():刪掉集合中的所有值。
list4.clear();
1
4.Set和數(shù)組的轉(zhuǎn)換:擴(kuò)展運(yùn)算符 Array.from()方法
const oArray = […list4];
const oArray = Array.from(list4);
1
2
3
let oSet = new Set([1,2,3]); // 數(shù)組 迭代對象
1
5.WeakSet
const weak = new WeakSet().add(array1).add(array2);
console.log(weak);
array1 = null;
array2 = null;
const array3 = […weak][0];
const array4 = […weak][1];
1
2
3
4
5
6
Map
1.創(chuàng)建Map
const romanNumerals = new Map();
1
2.方法和屬性
size屬性:獲取鍵和值的數(shù)量。
console.log(romanNumerals.size);
1
get(key):通過鍵獲取值。
console.log(romanNumerals.get(3));
1
has(key):檢測一個(gè)特定鍵是否在映射中。
console.log(romanNumerals.has(5));
1
delete(key):從映射中刪除一個(gè)鍵值對。
romanNumerals.delete(5);
1
clear():從映射中刪除所有鍵值對。
console.log(romanNumerals);
1
3.Map轉(zhuǎn)換為數(shù)組:擴(kuò)展運(yùn)算符 Array.from() 方法
const oArray1 = […romanNumerals];
const oArray2 = Array.from(romanNumerals);
1
2
4.WeakMap
// 內(nèi)存泄漏
strong1 = new Map().set(array5).set(array6); // 創(chuàng)建映射,添加數(shù)組。
weak1 = new WeakMap().set(array9).set(array10); // 創(chuàng)建映射,添加數(shù)組。
總結(jié)
以上是生活随笔為你收集整理的2-44钟静雯_day03的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十年风雨路,中国MES市场亟呼健康的生态
- 下一篇: 计算机主机寿命多长,一般电脑的寿命是多久