最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript 對(duì)象與數(shù)組操作大全

 更新時(shí)間:2026年02月26日 16:20:58   作者:竟未曾年少輕狂  
本文介紹了JavaScript中的對(duì)象和數(shù)組,包括它們的基本概念、創(chuàng)建方法、屬性操作、遍歷方法、增刪改操作以及常用方法,同時(shí),還討論了原型和繼承、對(duì)象的拷貝和方法簡(jiǎn)寫等高級(jí)技巧,感興趣的朋友跟隨小編一起看看吧

對(duì)象

一、對(duì)象基礎(chǔ)概念

對(duì)象是 JavaScript 的核心數(shù)據(jù)類型,用于存儲(chǔ)鍵值對(duì)集合??梢岳斫鉃楝F(xiàn)實(shí)事物的代碼映射。

// 一個(gè)簡(jiǎn)單的用戶對(duì)象
let user = {
    name: "張三",        // 屬性:鍵值對(duì)
    age: 25,
    "full name": "張三豐", // 包含空格的屬性名需要引號(hào)
    sayHi: function() {    // 方法:值為函數(shù)的屬性
        return "你好";
    }
};
  • 使用對(duì)象字面量創(chuàng)建對(duì)象,避免 new Object()
  • 屬性名使用駝峰命名,除非必要不使用引號(hào)
  • 使用解構(gòu)賦值提高代碼可讀性
  • 注意區(qū)分淺拷貝和深拷貝
  • 使用 Object.freeze() 保護(hù)配置對(duì)象
  • 遍歷對(duì)象優(yōu)先使用 Object.keys() 和 for...of

二、創(chuàng)建對(duì)象

1. 對(duì)象字面量(最常用)

字面量語(yǔ)法是創(chuàng)建對(duì)象最直接的方式,它允許我們?cè)诖罄ㄌ?hào)`{}`中直接定義對(duì)象的屬性和方法。

const person = {
    name: "李四",
    age: 30,
    job: "工程師"
};

2. 使用 new Object()

這種方式在某些特定場(chǎng)景下可能更為適用,例如在需要?jiǎng)討B(tài)創(chuàng)建對(duì)象并根據(jù)條件設(shè)置屬性時(shí)。

const person = new Object();
person.name = "王五";
person.age = 28;

3. 使用構(gòu)造函數(shù)

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.greet = function() {
        return `你好,我是${this.name}`;
    };
}
const p1 = new Person("趙六", 22);

4. 使用 Object.create()

const proto = {
    greet() {
        return "你好";
    }
};
const obj = Object.create(proto);
obj.name = "小明";

5. 使用類(ES6+)

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    greet() {
        return `你好,我是${this.name}`;
    }
}

三、屬性的增刪改查

1. 訪問(wèn)屬性

const user = {
    name: "小紅",
    age: 20,
    "my-email": "hong@example.com"
};
// 點(diǎn)號(hào)語(yǔ)法(最常用)
console.log(user.name);  // "小紅"
// 方括號(hào)語(yǔ)法(特殊屬性名、動(dòng)態(tài)屬性名時(shí)使用)
console.log(user["age"]);     // 20
console.log(user["my-email"]); // "hong@example.com"
// 動(dòng)態(tài)屬性名
const key = "name";
console.log(user[key]);  // "小紅"
// 可選鏈操作符(ES2020)- 避免訪問(wèn)不存在的屬性時(shí)報(bào)錯(cuò)
console.log(user?.address?.city);  // undefined(不會(huì)報(bào)錯(cuò))

2. 添加/修改屬性

向?qū)ο筇砑有聦傩苑浅:?jiǎn)單,只需使用賦值語(yǔ)句即可。如果屬性不存在,將會(huì)自動(dòng)創(chuàng)建該屬性并賦予相應(yīng)的值。

const car = {};
// 添加屬性
car.brand = "特斯拉";
car["model"] = "Model 3";
car.year = 2023;
// 修改屬性
car.year = 2024;
// 使用變量作為屬性名
const propName = "color";
car[propName] = "紅色";
console.log(car);
// { brand: "特斯拉", model: "Model 3", year: 2024, color: "紅色" }

3. 刪除屬性

使用`delete`運(yùn)算符可以刪除對(duì)象的屬性。刪除后,該屬性將不再屬于對(duì)象,訪問(wèn)時(shí)會(huì)返回`undefined`。需要注意的是,`delete`操作符僅刪除對(duì)象自身的屬性,如果屬性是繼承而來(lái),則不會(huì)被刪除。

const student = {
    name: "小明",
    age: 18,
    score: 95,
    password: "123456"
};
// 刪除單個(gè)屬性
delete student.password;
// 刪除成功返回 true,屬性不存在也返回 true
console.log(delete student.age);  // true
console.log(student);  // { name: "小明", score: 95 }

四、屬性遍歷和檢查

1. 檢查屬性是否存在

const obj = { name: "測(cè)試", age: 25 };
// 使用 in 運(yùn)算符
console.log("name" in obj);    // true
console.log("job" in obj);     // false
// 使用 hasOwnProperty(檢查自身屬性,不包括原型鏈)
console.log(obj.hasOwnProperty("name"));  // true
console.log(obj.hasOwnProperty("toString")); // false(原型鏈上的方法)
// 直接判斷是否為 undefined(不嚴(yán)謹(jǐn),因?yàn)橹悼赡艽_實(shí)是 undefined)
console.log(obj.name !== undefined);  // true
console.log(obj.job !== undefined);   // false

2. 遍歷對(duì)象屬性

const person = {
    name: "張三",
    age: 30,
    job: "工程師"
};
// 1. for...in 循環(huán)(包括原型鏈上的可枚舉屬性)
for (let key in person) {
    console.log(key, person[key]);
}
// 2. Object.keys() - 返回自身屬性的鍵數(shù)組
console.log(Object.keys(person));    // ["name", "age", "job"]
// 3. Object.values() - 返回自身屬性的值數(shù)組
console.log(Object.values(person));  // ["張三", 30, "工程師"]
// 4. Object.entries() - 返回鍵值對(duì)數(shù)組
console.log(Object.entries(person)); 
// [["name", "張三"], ["age", 30], ["job", "工程師"]]
// 5. 遍歷 entries
Object.entries(person).forEach(([key, value]) => {
    console.log(`${key}: ${value}`);
});

五、對(duì)象方法

對(duì)象的方法是存儲(chǔ)在對(duì)象中的函數(shù),用于定義對(duì)象的行為。

1. 定義方法

在對(duì)象中定義方法時(shí),方法名后跟一個(gè)函數(shù)表達(dá)式。這樣,對(duì)象就擁有了執(zhí)行特定任務(wù)的能力。通過(guò)在對(duì)象中封裝方法,將數(shù)據(jù)和操作數(shù)據(jù)的函數(shù)緊密結(jié)合在一起,體現(xiàn)了面向?qū)ο缶幊痰乃枷搿?/p>

let calculator = {
    add: function(a, b) {
        return a + b;
    },
    subtract: function(a, b) {
        return a - b;
    }
};
console.log(calculator.add(5, 3)); // 輸出 8
console.log(calculator.subtract(5, 3)); // 輸出 2

使用箭頭函數(shù)定義方法

let mathUtils = {
    multiply: (a, b) => a * b,
    divide: (a, b) => a / b
};
console.log(mathUtils.multiply(4, 3)); // 輸出 12
console.log(mathUtils.divide(10, 2)); // 輸出 5

箭頭函數(shù)提供了一種更為簡(jiǎn)潔的方法定義方式。在對(duì)象的方法中使用箭頭函數(shù)時(shí),需要注意`this`的指向問(wèn)題。箭頭函數(shù)中的`this`會(huì)繼承其所在上下文的`this`值,這與傳統(tǒng)函數(shù)的`this`行為有所不同。因此,在需要正確引用對(duì)象自身屬性的情況下,需要謹(jǐn)慎使用箭頭函數(shù)。

2. 常用對(duì)象方法

const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
// Object.assign() - 合并對(duì)象
const merged = Object.assign({}, obj1, obj2);
console.log(merged);  // { a: 1, b: 2, c: 3, d: 4 }
// Object.freeze() - 凍結(jié)對(duì)象(不能修改、添加、刪除)
const frozen = Object.freeze({ name: "固定值" });
frozen.name = "新值";    // 嚴(yán)格模式下會(huì)報(bào)錯(cuò)
console.log(frozen.name); // "固定值"(沒(méi)變)
// Object.seal() - 密封對(duì)象(不能添加刪除,但可以修改)
const sealed = Object.seal({ count: 10 });
sealed.count = 20;       // 可以修改
delete sealed.count;     // 不能刪除
sealed.newProp = "新";   // 不能添加

3. 屬性描述符

const user = { name: "張三" };
// 獲取屬性描述符
const descriptor = Object.getOwnPropertyDescriptor(user, "name");
console.log(descriptor);
// {
//   value: "張三",
//   writable: true,   // 是否可修改
//   enumerable: true, // 是否可枚舉
//   configurable: true // 是否可配置(刪除、修改屬性特性)
// }
// 定義屬性的元數(shù)據(jù)
Object.defineProperty(user, "age", {
    value: 25,
    writable: false,   // 只讀
    enumerable: false, // 不可枚舉
    configurable: false // 不可配置
});
console.log(user.age);  // 25
user.age = 30;          // 修改無(wú)效(嚴(yán)格模式報(bào)錯(cuò))
console.log(user.age);  // 25
console.log(Object.keys(user)); // ["name"](age不可枚舉)

六、原型和繼承

// 原型鏈繼承
const parent = {
    greet() {
        return "你好";
    }
};
const child = Object.create(parent);
child.name = "孩子";
console.log(child.name);      // "孩子"(自身屬性)
console.log(child.greet());   // "你好"(原型上的方法)
console.log(child.toString()); // 原型鏈上的方法
// 獲取原型
console.log(Object.getPrototypeOf(child)); // parent對(duì)象
// 檢查是否是自身屬性
console.log(child.hasOwnProperty("name"));   // true
console.log(child.hasOwnProperty("greet"));  // false

七、對(duì)象操作

1. 拷貝對(duì)象

const original = { a: 1, b: { c: 2 } };
// 淺拷貝
const shallowCopy1 = Object.assign({}, original);
const shallowCopy2 = { ...original };
// 深拷貝
const deepCopy = JSON.parse(JSON.stringify(original));
// 注意:這種方法會(huì)丟失函數(shù)、undefined、Symbol等
// 簡(jiǎn)單深拷貝函數(shù)
function deepClone(obj) {
    if (obj === null || typeof obj !== "object") return obj;
    if (obj instanceof Date) return new Date(obj);
    if (obj instanceof Array) return obj.map(item => deepClone(item));
    const cloned = {};
    Object.keys(obj).forEach(key => {
        cloned[key] = deepClone(obj[key]);
    });
    return cloned;
}

2. 計(jì)算屬性名

const key = "dynamicKey";
const value = "動(dòng)態(tài)值";
const obj = {
    [key]: value,           // 計(jì)算屬性名
    [`${key}2`]: "另一個(gè)值",
    [1 + 2]: "三"           // 屬性名可以是表達(dá)式
};
console.log(obj);  // { dynamicKey: "動(dòng)態(tài)值", dynamicKey2: "另一個(gè)值", 3: "三" }

3. 方法簡(jiǎn)寫

const oldStyle = {
    name: "老方式",
    sayHi: function() {
        return "Hi";
    }
};
const newStyle = {
    name: "新方式",
    sayHi() {          // 方法簡(jiǎn)寫
        return "Hi";
    },
    // 生成器函數(shù)簡(jiǎn)寫
    *generator() {
        yield 1;
        yield 2;
    }
};

數(shù)組

一、數(shù)組基礎(chǔ)概念

數(shù)組是 JavaScript 中用于存儲(chǔ)有序集合的特殊對(duì)象??梢园我忸愋偷脑?。

二、創(chuàng)建數(shù)組

1. 數(shù)組字面量(最常用)

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [];  // 空數(shù)組
const arr3 = [1, "two", true, null, undefined, { name: "對(duì)象" }];

2. 使用 Array 構(gòu)造函數(shù)

const arr1 = new Array();  // []
const arr2 = new Array(5); // [empty × 5] 長(zhǎng)度為5的空數(shù)組
const arr3 = new Array(1, 2, 3); // [1, 2, 3]
// 注意陷阱
const arr4 = new Array(3);     // [empty × 3] - 長(zhǎng)度為3的空數(shù)組
const arr5 = [3];              // [3] - 包含一個(gè)元素3的數(shù)組

3. 使用 Array.of() (ES6)

const arr1 = Array.of(5);      // [5] - 解決了構(gòu)造函數(shù)的歧義
const arr2 = Array.of(1, 2, 3); // [1, 2, 3]

4. 使用 Array.from() (ES6)

// 從類數(shù)組對(duì)象創(chuàng)建
const str = "hello";
const arr1 = Array.from(str);  // ["h", "e", "l", "l", "o"]
// 從 Set 創(chuàng)建
const set = new Set([1, 2, 3]);
const arr2 = Array.from(set);  // [1, 2, 3]
// 使用映射函數(shù)
const arr3 = Array.from([1, 2, 3], x => x * 2); // [2, 4, 6]
// 創(chuàng)建指定范圍的數(shù)組
const range = Array.from({ length: 5 }, (_, i) => i + 1); // [1, 2, 3, 4, 5]

三、數(shù)組的基本操作

1. 訪問(wèn)和修改元素

const arr = ["a", "b", "c", "d"];
// 訪問(wèn)元素
console.log(arr[0]);     // "a" - 第一個(gè)元素
console.log(arr[arr.length - 1]); // "d" - 最后一個(gè)元素
console.log(arr[10]);    // undefined - 索引不存在
// 修改元素
arr[1] = "x";
console.log(arr);  // ["a", "x", "c", "d"]
// 添加元素到任意位置
arr[4] = "e";
console.log(arr);  // ["a", "x", "c", "d", "e"]
arr[10] = "z";
console.log(arr);  // ["a", "x", "c", "d", "e", empty × 5, "z"]

2. length 屬性

const arr = [1, 2, 3, 4];
console.log(arr.length);  // 4
// 截?cái)鄶?shù)組
arr.length = 2;
console.log(arr);  // [1, 2]
// 清空數(shù)組
arr.length = 0;
console.log(arr);  // []
// 增加長(zhǎng)度(會(huì)創(chuàng)建空位)
arr.length = 5;
console.log(arr);  // [empty × 5]

四、數(shù)組的遍歷方法

1. 傳統(tǒng) for 循環(huán)

const arr = ["a", "b", "c"];
// 基本for循環(huán)
for (let i = 0; i < arr.length; i++) {
    console.log(arr[i]);
}
// 反向遍歷
for (let i = arr.length - 1; i >= 0; i--) {
    console.log(arr[i]);
}

2. for...of 循環(huán) (ES6)

const arr = ["a", "b", "c"];
for (const item of arr) {
    console.log(item);
}
// 同時(shí)獲取索引和值
for (const [index, value] of arr.entries()) {
    console.log(index, value);
}

3. for...in 循環(huán)(不推薦用于數(shù)組)

const arr = ["a", "b", "c"];
// 遍歷索引(包括自定義屬性)
for (const index in arr) {
    console.log(index, arr[index]);
}

4. 數(shù)組內(nèi)置遍歷方法

const arr = [1, 2, 3, 4, 5];
// forEach - 遍歷每個(gè)元素
arr.forEach((item, index, array) => {
    console.log(`索引${index}: ${item}`);
});
// map - 映射為新數(shù)組
const doubled = arr.map(item => item * 2);
console.log(doubled);  // [2, 4, 6, 8, 10]
// filter - 過(guò)濾元素
const evens = arr.filter(item => item % 2 === 0);
console.log(evens);  // [2, 4]
// reduce - 累積計(jì)算
const sum = arr.reduce((acc, item) => acc + item, 0);
console.log(sum);  // 15
// some - 是否有元素滿足條件
const hasEven = arr.some(item => item % 2 === 0);
console.log(hasEven);  // true
// every - 是否所有元素滿足條件
const allPositive = arr.every(item => item > 0);
console.log(allPositive);  // true
// find - 查找第一個(gè)滿足條件的元素
const found = arr.find(item => item > 3);
console.log(found);  // 4
// findIndex - 查找第一個(gè)滿足條件的索引
const foundIndex = arr.findIndex(item => item > 3);
console.log(foundIndex);  // 3

五、數(shù)組的增刪改操作

1. 添加元素

const arr = [1, 2, 3];
// push - 末尾添加(返回新長(zhǎng)度)
arr.push(4, 5);
console.log(arr);  // [1, 2, 3, 4, 5]
// unshift - 開(kāi)頭添加(返回新長(zhǎng)度)
arr.unshift(-1, 0);
console.log(arr);  // [-1, 0, 1, 2, 3, 4, 5]
// splice - 任意位置添加
arr.splice(3, 0, "a", "b");  // 在索引3處插入"a","b"
console.log(arr);  // [-1, 0, 1, "a", "b", 2, 3, 4, 5]

2. 刪除元素

const arr = [1, 2, 3, 4, 5];
// pop - 刪除末尾元素(返回刪除的元素)
const last = arr.pop();
console.log(last, arr);  // 5, [1, 2, 3, 4]
// shift - 刪除開(kāi)頭元素(返回刪除的元素)
const first = arr.shift();
console.log(first, arr);  // 1, [2, 3, 4]
// splice - 刪除任意位置元素
const removed = arr.splice(1, 2);  // 從索引1開(kāi)始刪除2個(gè)
console.log(removed, arr);  // [3, 4], [2]
// 清空數(shù)組的幾種方式
let arr2 = [1, 2, 3];
arr2.length = 0;      // 方式1
arr2 = [];            // 方式2
arr2.splice(0);       // 方式3
while(arr2.length) {  // 方式4
    arr2.pop();
}

3. 修改元素

const arr = [1, 2, 3, 4, 5];
// 直接通過(guò)索引修改
arr[2] = 30;
// splice - 替換元素
arr.splice(1, 2, 20, 30);  // 從索引1開(kāi)始刪除2個(gè),插入20,30
console.log(arr);  // [1, 20, 30, 4, 5]
// fill - 填充元素(ES6)
const filled = new Array(5).fill(0);  // [0, 0, 0, 0, 0]
arr.fill(9, 1, 3);  // 從索引1到3填充9
console.log(arr);  // [1, 9, 9, 4, 5]

六、數(shù)組的常用方法

1. 合并與切割

const arr1 = [1, 2];
const arr2 = [3, 4];
const arr3 = [5, 6];
// concat - 合并數(shù)組(返回新數(shù)組)
const merged = arr1.concat(arr2, arr3);
console.log(merged);  // [1, 2, 3, 4, 5, 6]
// slice - 切割數(shù)組(返回新數(shù)組)
const sliced = merged.slice(1, 4);  // [2, 3, 4]
// join - 連接為字符串
const str = merged.join("-");  // "1-2-3-4-5-6"
// split - 字符串分割為數(shù)組(字符串方法)
const arr = "a,b,c".split(",");  // ["a", "b", "c"]

2. 查找與判斷

const arr = [1, 2, 3, 2, 1];
// indexOf - 查找元素第一次出現(xiàn)的索引
console.log(arr.indexOf(2));     // 1
console.log(arr.indexOf(5));     // -1
// lastIndexOf - 查找元素最后一次出現(xiàn)的索引
console.log(arr.lastIndexOf(2)); // 3
// includes - 是否包含元素(ES6)
console.log(arr.includes(3));    // true
console.log(arr.includes(5));    // false
// find - 查找滿足條件的第一個(gè)元素
const found = arr.find(item => item > 2);  // 3
// findIndex - 查找滿足條件的第一個(gè)索引
const foundIndex = arr.findIndex(item => item > 2);  // 2

3. 排序與反轉(zhuǎn)

const arr = [3, 1, 4, 1, 5, 9, 2, 6];
// sort - 排序(修改原數(shù)組)
arr.sort();
console.log(arr);  // [1, 1, 2, 3, 4, 5, 6, 9](默認(rèn)按字符串排序)
// 自定義排序
const numbers = [3, 1, 4, 1, 5, 9];
numbers.sort((a, b) => a - b);  // 升序
console.log(numbers);  // [1, 1, 3, 4, 5, 9]
numbers.sort((a, b) => b - a);  // 降序
console.log(numbers);  // [9, 5, 4, 3, 1, 1]
// reverse - 反轉(zhuǎn)數(shù)組
const arr2 = [1, 2, 3, 4];
arr2.reverse();
console.log(arr2);  // [4, 3, 2, 1]

4. 數(shù)組轉(zhuǎn)換

const arr = [1, 2, 3, 4, 5];
// map - 映射
const doubled = arr.map(x => x * 2);
// filter - 過(guò)濾
const evens = arr.filter(x => x % 2 === 0);
// reduce - 歸約
const sum = arr.reduce((acc, x) => acc + x, 0);
// flat - 扁平化數(shù)組(ES2019)
const nested = [1, [2, 3], [4, [5, 6]]];
const flat1 = nested.flat();        // [1, 2, 3, 4, [5, 6]]
const flat2 = nested.flat(2);       // [1, 2, 3, 4, 5, 6]
// flatMap - 映射后扁平化
const result = [1, 2, 3].flatMap(x => [x, x * 2]);
console.log(result);  // [1, 2, 2, 4, 3, 6]

七、多維數(shù)組

// 創(chuàng)建二維數(shù)組
const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
// 訪問(wèn)元素
console.log(matrix[1][2]);  // 6
// 遍歷二維數(shù)組
for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
        console.log(`matrix[${i}][${j}] = ${matrix[i][j]}`);
    }
}
// 創(chuàng)建指定大小的二維數(shù)組
function createMatrix(rows, cols, initial = 0) {
    return Array.from({ length: rows }, () => 
        Array.from({ length: cols }, () => initial)
    );
}
const matrix2 = createMatrix(3, 3, 0);
console.log(matrix2);  // [[0,0,0], [0,0,0], [0,0,0]]

八、數(shù)組的高級(jí)技巧

1. 數(shù)組去重

const arr = [1, 2, 2, 3, 3, 3, 4, 4, 5];
// 使用 Set
const unique1 = [...new Set(arr)];
console.log(unique1);  // [1, 2, 3, 4, 5]
// 使用 filter 和 indexOf
const unique2 = arr.filter((item, index) => arr.indexOf(item) === index);
// 使用 reduce
const unique3 = arr.reduce((acc, item) => {
    if (!acc.includes(item)) acc.push(item);
    return acc;
}, []);

2. 數(shù)組交集、并集、差集

const arr1 = [1, 2, 3, 4];
const arr2 = [3, 4, 5, 6];
// 并集
const union = [...new Set([...arr1, ...arr2])];
console.log(union);  // [1, 2, 3, 4, 5, 6]
// 交集
const intersection = arr1.filter(item => arr2.includes(item));
console.log(intersection);  // [3, 4]
// 差集(arr1中有但arr2中沒(méi)有)
const difference = arr1.filter(item => !arr2.includes(item));
console.log(difference);  // [1, 2]

3. 數(shù)組分組

const arr = [
    { name: "張三", age: 25, department: "技術(shù)部" },
    { name: "李四", age: 30, department: "市場(chǎng)部" },
    { name: "王五", age: 28, department: "技術(shù)部" },
    { name: "趙六", age: 35, department: "市場(chǎng)部" }
];
// 按部門分組
const grouped = arr.reduce((acc, person) => {
    const dept = person.department;
    if (!acc[dept]) {
        acc[dept] = [];
    }
    acc[dept].push(person);
    return acc;
}, {});
console.log(grouped);
// {
//   技術(shù)部: [{ name: "張三", ... }, { name: "王五", ... }],
//   市場(chǎng)部: [{ name: "李四", ... }, { name: "趙六", ... }]
// }

4. 數(shù)組打平與鏈?zhǔn)秸{(diào)用

const data = [
    { category: "水果", items: ["蘋果", "香蕉"] },
    { category: "蔬菜", items: ["白菜", "蘿卜"] }
];
// 獲取所有物品
const allItems = data
    .flatMap(category => category.items)
    .map(item => item.toUpperCase())
    .filter(item => item.length > 2)
    .sort();
console.log(allItems);  // ["蘋果", "香蕉", "白菜", "蘿卜"](按拼音排序)

九、類數(shù)組對(duì)象

// 常見(jiàn)的類數(shù)組對(duì)象
function example() {
    console.log(arguments);  // 類數(shù)組對(duì)象
    console.log(Array.isArray(arguments));  // false
    // 轉(zhuǎn)換為真實(shí)數(shù)組
    const args1 = Array.from(arguments);
    const args2 = [...arguments];
    const args3 = Array.prototype.slice.call(arguments);
}
example(1, 2, 3);
// DOM 元素集合
const divs = document.querySelectorAll('div');
const divArray = Array.from(divs);  // 轉(zhuǎn)換為數(shù)組

到此這篇關(guān)于JavaScript 對(duì)象與數(shù)組的文章就介紹到這了,更多相關(guān)js對(duì)象與數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

庆元县| 家居| 肥乡县| 屏边| 琼中| 凤庆县| 河西区| 喀喇| 清涧县| 墨玉县| 五常市| 积石山| 福贡县| 湖北省| 东台市| 本溪| 抚顺县| 马公市| 綦江县| 龙海市| 迁西县| 来宾市| 盐城市| 林周县| 洪泽县| 黔西县| 花莲县| 高台县| 丹阳市| 屯昌县| 香河县| 卢龙县| 高安市| 蓝田县| 喀喇沁旗| 府谷县| 永泰县| 盐边县| 广宗县| 扬州市| 吉安县|