前端函数汇总

获得本周,周一和周日

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("未找到。");
}

本文作者:Jason.裕哥

本文链接:https://www.cnblogs.com/fuyu-blog/p/17504718.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Jason.裕哥  阅读(31)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起