前端函数汇总

获得本周,周一和周日

let now = new Date();  
let nowTime = now.getTime();    
let day = now.getDay();
let oneDayTime = 24 * 60 * 60 * 1000;
let MondayTime = nowTime - (day - 1) * oneDayTime;
let SundayTime = nowTime + (7 - day) * oneDayTime;

获得本月1号和月底日期

const today = new Date();
const firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);

在数组内,根据对象某个字段值查询,并获得索引和对象

const fieldName = "targetField";
const searchValue = "desiredValue";
const array = [/* your array of objects */];

const index = array.findIndex(obj => obj[fieldName] === searchValue);
if (index !== -1) {
  console.log("找到了!索引为:" + index);
} else {
  console.log("未找到。");
}

const obj = array.find(obj => obj[fieldName] === searchValue);
if (obj !== undefined) {
  console.log("找到了!对象为:" + obj);
} else {
  console.log("未找到。");
}
posted @ 2023-08-07 11:18  Jason.裕哥  阅读(26)  评论(0编辑  收藏  举报