跳转至

关于

一些关于 JavaScript 的内容

随机生成手机号

function generateRandomPhone() {
  var validAreaCodes = ['130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
    '150', '151', '152', '153', '155', '156', '157', '158', '159',
    '180', '181', '182', '183', '184', '185', '186', '187', '188', '189'];

  var areaCode = validAreaCodes[Math.floor(Math.random() * validAreaCodes.length)];
  var phone = areaCode;

  for (var i = 0; i < 8; i++) {
    phone += Math.floor(Math.random() * 10);
  }

  return phone;
}

js数组转字符串

var numbers = [1, 2, 3, 4, 5];
var str = numbers.join(",");
console.log(str); // "1,2,3,4,5"

js数组去重

var arr = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
var newArr = arr.filter(function(item, index, arr) {
  return arr.indexOf(item) === index;
});
console.log(newArr); // [1, 2, 3, 4, 5]

js数组去空格

var str = "  hello world  ";
var newStr = str.replace(/\s/g, "");
console.log(newStr); // "helloworld"

删除某个属性

去除 phone 返回新的对象

const obj = {
    name: 'wang',
    age: 18,
    sex: "男",
    phone: 18888888888
}
const {phone, ...rest} = obj;
console.log(rest)  // {"name": "wang","age": 18,"sex": "男"}

时间戳

new Date().getTime()