手机版 欢迎访问it开发者社区(www.mfbz.cn)网站

当前位置: > 开发

常用的js代码以及在线生成工具地址

时间:2021/9/22 11:56:24|来源:|点击: 次

1:数组求和使用reduce

let nums = [1,22,31,4,56]
let sum = nums.reduce((prev, cur) => prev + cur, 0)

2:数组去重

let arr = [1,1,2,434,2,1]
console.log([...new Set(arr)]) // 1 2 434

3:生成随机Id

Math.random().toString(36).substr(2)

4:获取对象key键值

let person = {name: "蛙人", age: 24};
console.log(Object.keys(person)) // ["name", "age"]

5:获取对象value值

let person = {name: "蛙人", age: 24};
console.log(Object.values(person)) // ["蛙人", 24]

6:获取对象里指定的值

const person = {name: "蛙人", age: 24, sex: "male"};
let { age, sex } = person
console.log(age, sex) // 24 male

7:模板字符串表达式

let name = "蛙人"
console.log(`hello ${name}`)
console.log(`<p>${name}</p>`)

Copyright © 2002-2019 某某自媒体运营 版权所有