js常用方法(附代码解析)

字符串处理方法

• String.prototype.includes(): 检查一个字符串是否包含另一个字符串。

• String.prototype.indexOf(): 查找子字符串在字符串中的位置,返回索引。

• String.prototype.slice(): 截取字符串的一部分并返回。

• String.prototype.replace(): 替换字符串中的指定部分。

• String.prototype.split(): 将字符串分割成数组

let init = 'Hello world!';
    console.log(init.includes('world')); // 输出    true
    console.log(init.indexOf('w')); // 输出    6
    console.log(init.slice(6)); // 输出    world!
    console.log(init.replace('world','世界')); // 输出    Hello,世界!
    console.log(init.split(' ',2)); // 输出    [ 'Hello', 'world!' ]

数组处理方法

• Array.prototype.push(): 向数组的末尾添加一个或多个元素。

• Array.prototype.pop(): 删除数组的最后一个元素并返回该元素。

• Array.prototype.shift(): 删除数组的第一个元素并返回该元素。

• Array.prototype.unshift(): 在数组的开头添加一个或多个元素。

• Array.prototype.map(): 遍历数组并对每个元素执行指定的操作,返回一个新数组。

• Array.prototype.filter(): 过滤数组中的元素并返回满足条件的元素的新数组。

• Array.prototype.reduce(): 对数组中的元素进行累加操作,最终返回一个累加值。

• Array.prototype.find(): 找到并返回第一个符合条件的数组元素。

• Array.prototype.forEach(): 遍历数组,对每个元素执行指定操作,但不返回值。

let arr = [1, 2, 3];
    console.log(arr.push(4), '-----', arr); // 输出    4 '-----' [1, 2, 3, 4]
    let arr2 = [1, 2, 3];
    console.log(arr2.pop(), '-----', arr2); // 输出    3 '-----' [1, 2]
    let arr3 = [1, 2, 3];
    console.log(arr3.shift(), '-----', arr3); // 输出    1 '-----' [2, 3]
    let arr4 = [1, 2, 3];
    console.log(arr4.unshift(0), '-----', arr4); // 输出    4 '-----' [0,1, 2, 3]
    let arr5 = [1, 2, 3];
    console.log(arr5.map(item => item * 2), '-----', arr5); // 输出新数组    [2,4,6] '-----' 原数组[1, 2, 3]
    let arr6 = [1, 2, 3];
    console.log(arr6.filter(item => item > 1)); // 输出    [2,3]
    let arr7 = [1, 2, 3];
    console.log(arr7.reduce((pre, cur) => pre + cur)); // 输出    6
    let arr8 = [1, 2, 3];
    console.log(arr8.find(item => item > 1)); // 输出    2
    let arr9 = [1, 2, 3];
    console.log(arr9.forEach(item => item * 2)); // 输出    undefined

对象处理方法

• Object.keys(): 返回对象的所有可枚举属性名组成的数组。

• Object.values(): 返回对象的所有可枚举属性值组成的数组。

• Object.entries(): 返回对象的所有键值对组成的数组。

• Object.assign(): 将一个或多个源对象的属性复制到目标对象中。

• Object.freeze(): 冻结对象,使其不可修改。

 const person = { name: "Alice", age: 25, city: "New York" };
    console.log(Object.keys(person)); // 输出    [ 'name', 'age', 'city' ]
    console.log(Object.values(person)); // 输出    [ 'Alice', 25, 'New York' ]
    console.log(Object.entries(person)); // 输出    [ [ 'name', 'Alice' ], [ 'age', 25 ], [ 'city', 'New York' ] ]
    const target = { name: "Alice", age: 25 };
    const source = { age: 30, city: "New York" };
    const result = Object.assign(target, source);
    console.log(result); // 输出    { name: 'Alice', age: 30, city: 'New York' }
    // Object.freeze
    Object.freeze(person);
    person.age = 30; // 不会生效
    person.city = "New York"; // 不会生效
    delete person.name; // 不会生效
    console.log(person);
    // 输出: { name: "Alice", age: 25 }

JSON 处理方法

• JSON.parse(): 将 JSON 字符串解析为 JavaScript 对象。

• JSON.stringify(): 将 JavaScript 对象序列化为 JSON 字符串。

   // 	JSON.parse():将从服务器接收的 JSON 数据转换为 JavaScript 对象,以便在代码中使用。
    const jsonString = '{"name": "Alice", "age": 25, "city": "New York"}';
    const obj = JSON.parse(jsonString);
    console.log(obj);
    
	// JSON.stringify():将 JavaScript 对象转换为 JSON 字符串,以便将其发送到服务器或存储在本地。
    const obj2 = { name: "Alice", age: 25, city: "New York" };
    const jsonString2 = JSON.stringify(obj2);
    console.log(jsonString2);
    // 输出: '{"name":"Alice","age":25,"city":"New York"}'

定时器方法

• setTimeout(): 在指定的时间后执行一次函数。

• setInterval(): 每隔指定的时间重复执行函数

    setTimeout(() => {
        console.log('100毫秒后仅执行一次的定时器');
    }, 100);

    setInterval(() => {
        console.log('1000毫秒后重复执行的定时器');
    }, 1000);

数学方法

• Math.random(): 返回一个介于 0 和 1 之间的随机数。

const randomNumber = Math.random();
console.log(randomNumber); 
// 输出: 例如 0.3575814217712247(每次调用结果不同)

const min = 1;
const max = 10;
const randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(randomInt); 
// 输出: 1 到 10 之间的一个随机整数

• Math.floor(): 向下取整。

const num = 4.7;
const result = Math.floor(num);
console.log(result); 
// 输出: 4

• Math.ceil(): 向上取整。

const num = 4.3;
const result = Math.ceil(num);
console.log(result); 
// 输出: 5

• Math.round(): 四舍五入。

const num1 = 4.5;
const result1 = Math.round(num1);
console.log(result1); 
// 输出: 5

const num2 = 4.4;
const result2 = Math.round(num2);
console.log(result2); 
// 输出: 4

• Math.max(): 返回一组数中的最大值。

const max = Math.max(3, 6, 2, 9, 4);
console.log(max); 
// 输出: 9

// 从数组中获取最大值
const numbers = [3, 6, 2, 9, 4];
const max = Math.max(...numbers);
console.log(max); 
// 输出: 9

• Math.min(): 返回一组数中的最小值。

const min = Math.min(3, 6, 2, 9, 4);
console.log(min); 
// 输出: 2

// 从数组中获取最小值

const numbers = [3, 6, 2, 9, 4];
const min = Math.min(...numbers);
console.log(min); 
// 输出: 2

  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值