文章阅读

沉浸阅读完整内容

写文章

Vue3中判断对象中是否存在指定参数

正在统计阅读人数 正在统计字数
登录收藏0
字号 100%

export function checkParams(data, targetParam) {
  const keys = Object.keys(data);
  if (keys.includes(targetParam) && keys.every(key => key === targetParam || data[key] === "")) {
    return true; // 指定参数存在且没有其他参数或其他参数值为空时返回 true
  } else {
    return false; // 其他情况返回 false
  }
};

参数说明:


data:传入一个对象
targetParam:传入要校验的参数

使用示例:


let data1 = {
  operatorTime: '123123'
};
let targetParam1 = 'operatorTime';
console.log(checkParams(data1, targetParam1)); // 输出 true

let data2 = {
  serviceNumber: '',
  operatorTime: ''
};
let targetParam2 = 'operatorTime';
console.log(checkParams(data2, targetParam2)); // 输出 false

let data3 = {
  serviceNumber: '',
  operatorTime: '456456'
};
let targetParam3 = 'operatorTime';
console.log(checkParams(data3, targetParam3)); // 输出 true

推荐阅读

4 篇精选
文章 / 108字母掉落js 文章 / 107Vue2的双向数据绑定原理及缺陷 文章 / 105echarts如何根据窗口大小自适应 文章 / 106解决vuex刷新页面数据丢失

发表评论

评论 0

还没有评论,欢迎留下第一条讨论。

分享精彩,传递价值

生成专属海报,分享你的文章,让更多人发现好内容

浏览器不支持 Canvas,无法生成海报。

每日签到

每天来这里,记录一点持续积累。

当前积分--积分
本月签到--
本月获得--积分
正在读取