JS基礎面試題——日期、Math、數(shù)組、對象

主要介紹一下日期和Math及數(shù)組和對象的api,還有三道簡單的面試題

知識點:

1. 日期
console.log(Date.now());  // 獲取當前毫秒數(shù)
var dt = new Date();  // 獲取當前時間
console.log(dt.getTime());  // 當前時間的毫秒數(shù)
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. 常用的數(shù)組api
  • forEach(遍歷所有元素)
var arr = ['a', 'b', 'c', 'd'];
arr.forEach(function (item, index) {
  console.log(item + ',' + index);
})
  • map(對數(shù)組進行重新組裝,生成新的數(shù)組)
// map,生成新數(shù)組,不改變原來數(shù)組的格式
var arr = ['a', 'b', 'c', 'd'];
var result = arr.map(function (item, index) {
  return index + '/' + item;
})
console.log(result);
  • sort(對數(shù)組進行排序)
// sort, 會改變原來數(shù)組
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(根據(jù)條件對數(shù)組組合成字符串)
var arr = [1, 2, 3, 4];
var result = arr.join(',');
console.log(result);
  • reverse(將數(shù)組反轉)
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. 獲取隨機數(shù),要求長度一致的字符串格式
function randomStr(len) {
  var random = Math.random();
  random = random + '0000000000'; // 防止自動生成的數(shù)字不滿足長度報錯并且強制轉換成字符串
  return random.substr(0, len)
}

console.log(randomStr(20));
3. 寫一個能遍歷對象和數(shù)組的通用forEach函數(shù)
  1. 首先進行判斷是數(shù)組還是對象
  2. 根據(jù)不同的類型進行不同的循環(huán)解析
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);
});
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容