美文网首页
JS基础面试题——日期、Math、数组、对象

JS基础面试题——日期、Math、数组、对象

作者: 石燕平_Leo | 来源:发表于2018-05-06 14:21 被阅读0次

主要介绍一下日期和Math及数组和对象的api,还有三道简单的面试题

知识点:

1. 日期
console.log(Date.now());  // 获取当前毫秒数
var dt = new Date();  // 获取当前时间
console.log(dt.getTime());  // 当前时间的毫秒数
console.log(dt.getFullYear());  //  年
console.log(dt.getMonth()+1); // 月(0-11)
console.log(dt.getDate());  // 日(0-31)
console.log(dt.getHours()); // 时(0-23)
console.log(dt.getMinutes()); // 分(0-59)
console.log(dt.getSeconds()); // 秒(0-59)
2. Math

Math.random()

3. 常用的数组api
  • forEach(遍历所有元素)
var arr = ['a', 'b', 'c', 'd'];
arr.forEach(function (item, index) {
  console.log(item + ',' + index);
})
  • map(对数组进行重新组装,生成新的数组)
// map,生成新数组,不改变原来数组的格式
var arr = ['a', 'b', 'c', 'd'];
var result = arr.map(function (item, index) {
  return index + '/' + item;
})
console.log(result);
  • sort(对数组进行排序)
// sort, 会改变原来数组
var arr = [1, 23, 3, 4];
var result = arr.sort(function (a, b) {
  // 从小到大排序
  return a - b;

  // 从大到小排序
  // return b - a;
})
console.log(result);
  • filter(过滤符合条件的元素)
var arr = [1, 2, 3, 4];
var result = arr.filter(function (item, index) {
  if (item < 3) {
    return true
  }
})
console.log(result);
  • every(判断所有元素是否都符合要求)
var arr = [1, 2, 3, 4];
var result = arr.every(function (item, index) {
  if (item < 3) {
    return true
  }
})
console.log(result);   // false
  • some(判断是否有至少一个元素符合条件)
var arr = [1, 2, 3, 4];
var result = arr.some(function (item, index) {
  if (item < 3) {
    return true
  }
})
console.log(result);  // true
  • join(根据条件对数组组合成字符串)
var arr = [1, 2, 3, 4];
var result = arr.join(',');
console.log(result);
  • reverse(将数组反转)
var arr = [1, 2, 3, 4];
var result = arr.reverse();
console.log(result);
4. 常用的对象api
  • for in
  • hasOwnProperty(检查属性是不是对象自有的,排除从原型链找到的属性)
 var obj = {
  x: 10,
  y: 20,
  z: 30
}

for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    console.log(key + ':' + obj[key]);
  }
}

问题:

1. 获取当前日期(格式:2018-08-08)
function formatDate(dt) {
  if (!dt) {
    dt = new Date();
  }
  var year = dt.getFullYear();
  var month = dt.getMonth() + 1;
  var date = dt.getDate();

  if (month < 10) {
    month = '0' + month;
  }

  if (date < 10) {
    date = '0' + date;
  }

  return year + '-' + month + '-' + date;
}

var nowDate = new Date();
var formatDate = formatDate(nowDate);
console.log(formatDate);
2. 获取随机数,要求长度一致的字符串格式
function randomStr(len) {
  var random = Math.random();
  random = random + '0000000000'; // 防止自动生成的数字不满足长度报错并且强制转换成字符串
  return random.substr(0, len)
}

console.log(randomStr(20));
3. 写一个能遍历对象和数组的通用forEach函数
  1. 首先进行判断是数组还是对象
  2. 根据不同的类型进行不同的循环解析
function forEach(obj, fn) {
  if (obj instanceof Array) {
    obj.forEach(function (item, index) {
      fn(index, item);
    })
  } else {
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        fn(key, obj[key]);
      }
    }
  }
}

var arr = [1, 2, 3, 4];
forEach(arr, function (index, item) {
  console.log(index + ',' + item);
});

var obj = {
  x: 10,
  y: 20
};
forEach(obj, function (index, item) {
  console.log(index + ',' + item);
});

相关文章

  • JS基础面试题——日期、Math、数组、对象

    主要介绍一下日期和Math及数组和对象的api,还有三道简单的面试题 知识点: 1. 日期 2. Math Mat...

  • 数组&字符串方法&Math&Date

    数组方法 String 对象方法 数学Math方法 日期对象Date方法 Number对象方法 function对...

  • Math对象、数组、日期函数

    一、Math习题练习 Math是JavaScript的内置对象,提供一系列数学常数和数学方法。该对象不是构造函数,...

  • Python(四十)JavaScript进阶

    Python(四十)JavaScript进阶 这篇文章主要涉及的内容是JS中的内置对象(Math对象以及日期对象)...

  • 数组- js课程

    js课程———数组 by——曾庆林 1,复习内置对象(15分) String Date Math setInter...

  • 内置对象

    内置对象有 数学对象(Math对象), 日期对象(Date对象),还有String对象 Math对象: a...

  • JS数学对象Math、日期Date

    JS对象分为三种,分别是自定义对象、内置对象及浏览器对象。 内置对象就是JS语言自带的一些对象。 一、数学对象Ma...

  • 数组,日期,Math

    1、写一个函数,返回从min到max之间的 随机整数,包括min不包括max 2、写一个函数,返回从min都max...

  • JavaScript中数组的基本方法详解

    JS中的内置对象有:String,Array,Math,Date。今天我们来详解一下数组的创建及其方法。 数组的创...

  • JS基础-Math数组Date

    一.Math 1、写一个函数,返回从min到max之间的 随机整数,包括min不包括max 2、写一个函数,返回从...

网友评论

      本文标题:JS基础面试题——日期、Math、数组、对象

      本文链接:https://www.haomeiwen.com/subject/bnkurftx.html