1

53 阅读1分钟

数组去重

const arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]
const newArr = [...new Set(arr)]
console.log(newArr);
# 打印结果为[1,2,3,4,5]

遍历对象的所有属性名

const obj = {
  a: 1,
  b: 2,
  c: 3,
};
Object.keys(obj).forEach(function (item) {
  console.log(item);
})
for (const key in obj) {
  console.log(key);
}
for (const key of Object.keys(obj)) {
  console.log(key);
}

ES6求和函数

function sum(...args) {
    console.log(args.reduce((a, b) => a + b))
}
sum(1, 8, 56)

打开数组中的数组

var arr = [[1, 2, 3], [2, 3, 4]]
console.log(arr.flat());

打印结果为 [ 1, 2, 3, 2, 3, 4 ]