一、数组方法及示例
- push() 和 pop():在数组末尾添加元素(push)或删除末尾元素(pop)。
const arr = [1, 2, 3];
arr.push(4); // [1, 2, 3, 4]
arr.pop(); // [1, 2, 3]
- unshift() 和 shift():在数组开头添加元素(unshift)或删除开头元素(shift)。
arr.unshift(0); // [0, 1, 2, 3]
arr.shift(); // [1, 2, 3]
const arr1 = [1, 2];
const arr2 = [3, 4];
const merged = arr1.concat(arr2); // [1, 2, 3, 4]
- slice():返回数组的一部分,不会改变原数组。
const subArray = arr.slice(1, 3); // [2, 3]
- splice():在指定位置插入、删除或替换元素,会改变原数组。
arr.splice(1, 1); // 删除第二个元素,arr变为 [1, 3]
arr.splice(1, 0, 2); // 在第二个位置插入元素 2,arr变为 [1, 2, 3]
- indexOf() 和 lastIndexOf():查找元素在数组中的位置。
const index = arr.indexOf(2); // 1
const lastIndex = arr.lastIndexOf(2); // 1
- find() 和 findIndex():根据条件查找元素或元素索引。
const found = arr.find(element => element > 1); // 2
const foundIndex = arr.findIndex(element => element > 1); // 1
const filtered = arr.filter(element => element > 1); // [2, 3]
- map():对数组中的每个元素执行函数,并返回新数组。
const mapped = arr.map(element => element * 2); // [2, 4, 6]
- forEach():对数组中的每个元素执行函数,没有返回值。
arr.forEach(element => console.log(element)); // 1, 2, 3 (依次输出)
- reduce()、reduceRight()、reduce():将数组元素合并为一个值,从左到右(或从右到左)进行处理。
const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); // 6
- some() 和 every():检查数组中是否至少有一个元素满足条件(some),或所有元素都满足条件(every)。
const hasEven = arr.some(element => element % 2 === 0); // true
const allEven = arr.every(element => element % 2 === 0); // false
const sorted = arr.sort(); // [1, 2, 3]
const reversed = arr.reverse(); // [3, 2, 1]
const hasElement = arr.includes(2); // true
const isArray = Array.isArray(arr); // true
const joined = arr.join(', '); // "1, 2, 3"
- toString():将数组转换为字符串,等同于使用
join()
方法。
const arrToString = arr.toString(); // "1, 2, 3"
const filled = arr.fill(0); // [0, 0, 0]
- flat() 和 flatMap():将嵌套数组展平(flat)或对数组中的每个元素执行映射函数后展平(flatMap)。
const nestedArray = [1, [2, 3], [4, [5]]];
const flattened = nestedArray.flat(2); // [1, 2, 3, 4, 5]
const arr = [1, 2, 3]
const mappedAndFlattened = arr.flatMap(element => [element * 2]); // [2, 4, 6]
const numbers = [1, 2, 3];
const mappedAndFlattened = numbers.flatMap(num => [num, num * 2]); // [1, 2, 2, 4, 3, 6]
二、对象的方法及示例
- Object.keys()、Object.values()、Object.assign():返回对象自身可枚举属性的键、值和键值对数组。
const person = { name: 'Alice', age: 30 };
const keys = Object.keys(person); // ["name", "age"]
const entries = Object.entries(person); // [["name", "Alice"], ["age", 30]]
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
const merged = Object.assign(target, source); // { a: 1, b: 3, c: 4 }
- Object.entries():返回对象自身可枚举属性的键值对数组。
const person = { name: 'Alice', age: 30 };
const entries = Object.entries(person); // [["name", "Alice"], ["age", 30]]
- Object.defineProperty():定义或修改对象的属性。
const obj = {};
Object.defineProperty(obj, 'name', { value: 'John', writable: false });
- Object.getOwnPropertyDescriptor():获取对象的属性描述符。
const obj = { name: 'Alice' };
const descriptor = Object.getOwnPropertyDescriptor(obj, 'name');
- Object.freeze():冻结对象,防止修改属性或添加新属性。
const obj = { prop: 'value' };
const frozenObj = Object.freeze(obj);
- Object.seal():封闭对象,防止删除属性,但允许修改属性值。
const obj = { prop: 'value' };
const sealedObj = Object.seal(obj);
- Object.is():比较两个值是否相等,类似于严格相等运算符(===)。
console.log(Object.is(5, 5)); // true
- Object.hasOwnProperty():检查对象是否具有指定属性,不包括原型链上的属性。
const person = { name: 'Alice' };
console.log(person.hasOwnProperty('name')); // true
- Object.setPrototypeOf():设置对象的原型。
const person = { name: 'Alice' };
const employee = { job: 'Engineer' };
Object.setPrototypeOf(employee, person);
- Object.getPrototypeOf():获取对象的原型。
const person = { name: 'Alice' };
const employee = { job: 'Engineer' };
Object.setPrototypeOf(employee, person);
const proto = Object.getPrototypeOf(employee); // { name: 'Alice' }
- Object.create():使用指定的原型创建新对象。
const person = { name: 'Alice' };
const alice = Object.create(person);
- Object.getOwnPropertyNames():返回对象自身所有属性的键数组,包括不可枚举属性。
const person = { name: 'Alice', age: 30 };
const propertyNames = Object.getOwnPropertyNames(person); // ["name", "age"]
- Object.isExtensible():检查对象是否可扩展(即是否可以添加新属性)。
const obj = { prop: 'value' };
console.log(Object.isExtensible(obj)); // true
- Object.preventExtensions():阻止对象添加新属性。
const obj = { prop: 'value' };
Object.preventExtensions(obj);
- Object.isSealed():检查对象是否封闭,即属性不可删除且可修改。
const obj = { prop: 'value' };
console.log(Object.isSealed(obj)); // false
- Object.isFrozen():检查对象是否被冻结,即属性不可删除且不可修改。
const obj = { prop: 'value' };
console.log(Object.isFrozen(obj)); // false
- Object.getOwnPropertyDescriptors():获取对象所有属性的描述符。
const obj = { name: 'Alice', age: 30 };
const descriptors = Object.getOwnPropertyDescriptors(obj);
// descriptors =
{
name: {
value: 'Alice',
writable: true,
enumerable: true,
configurable: true
},
age: {
value: 30,
writable: true,
enumerable: true,
configurable: true
}
}
- Object.fromEntries():将键值对数组转换为对象。
const entries = [['name', 'Alice'], ['age', 30]];
const obj = Object.fromEntries(entries);
// obj = {name: "Alice", age: 30}