js various simple event processing (organizing)


## **js 删除对象中的某一个参数**

let pages={
    
    
id:111,
name:‘serena’,
type:1
}

移除对象中某个参数-移除参数-移除某个参数
需要删除 pages.type

移除参数  移除传参

delete pages.type


## **移除数组中的空值**

var list = [{
    
    
	area: "",
	content: "",
	daily_id: 124,
	id: 85,
	member_bar: "3333",
	module_id: 7,
	module_name: "xmn项目模块1",
	normal_time: 8,
	overtime: 2,
	position_id: 3,
}, {
    
    
	area: "",
	content: "",
	daily_id: 124,
	id: 85,
	member_bar: "3333",
	module_id: 7,
	module_name: "xmn项目模块1",
	normal_time: 8,
	overtime: 2,
	position_id: 3,
	aa: '',
	bb: '',
}];

var filteredList = list.map(function(item) {
    
    
	var newItem = {
    
    };
	for (var key in item) {
    
    
		if (item.hasOwnProperty(key) && item[key] !== "") {
    
    
			newItem[key] = item[key];
		}
	}
	return newItem;
});

console.log(filteredList);
结果:
[{
    
    
    daily_id: 124,
    id: 85,
    member_bar: "3333",
    module_id: 7,
    module_name: "xmn项目模块1",
    normal_time: 8,
    overtime: 2,
    position_id: 3,
}, {
    
    
    daily_id: 124,
    id: 85,
    member_bar: "3333",
    module_id: 7,
    module_name: "xmn项目模块1",
    normal_time: 8,
    overtime: 2,
    position_id: 3,
}]

**

## 获取当天昨天日期

**
// 当天日期
const today = new Date();

// 格式化当天日期为 YYYY-MM-DD 格式
const formattedToday = today.toISOString().slice(0, 10);

// 昨天日期
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

// 格式化昨天日期为 YYYY-MM-DD 格式
const formattedYesterday = yesterday.toISOString().slice(0, 10);

console.log("当天日期:", formattedToday);
console.log("昨天日期:", formattedYesterday);

Insert image description here

数组转json,json转数组
以下是将数组转换为JSON字符串的示例代码:
const array = [1, 2, 3, 4, 5];
const jsonString = JSON.stringify(array);
console.log(jsonString); // 输出:"[1,2,3,4,5]"

在这个示例中,数组[1, 2, 3, 4, 5]通过调用JSON.stringify()方法转换为JSON字符串"[1,2,3,4,5]"。

以下是将JSON字符串转换为数组的示例代码:
const jsonString = '[1, 2, 3, 4, 5]';
const array = JSON.parse(jsonString);
console.log(array); // 输出:[1, 2, 3, 4, 5]

在这个示例中,JSON字符串'[1, 2, 3, 4, 5]'通过调用JSON.parse()方法转换为数组[1, 2, 3, 4, 5]。
请注意,传递给JSON.parse()方法的字符串必须是有效的JSON格式,否则将引发语法错误。确保提供的字符串符合JSON规范,包括正确的语法、引号使用等。

## **js判断数组中是否包含-一个值**

const myArray = [1, 2, 3];
if (myArray.includes(2)) {
    
    
  console.log('myArray 包含数字 2');
} else {
    
    
  console.log('myArray 不包含数字 2');
}

## **//获取数组中-出现重复次数**

const array = ['我', '爱', '我', '家', '我', '爱', '哈'];
const countMap = {
    
    };
array.forEach(value => {
    
    
	if (countMap[value]) {
    
    
		countMap[value]++;
	} else {
    
    
		countMap[value] = 1;
	}
});
console.log(countMap, '统计结果,每个值出现的次数'); // 统计结果,每个值出现的次数
var obj = {
    
    
	我: 3,
	爱: 2,
	家: 1,
	哈: 1
};

var flattenedArray = Object.entries(obj).flatMap(([key, value]) => ({
	key,
	value
}));

console.log(flattenedArray);
```![在这里插入图片描述](https://img-blog.csdnimg.cn/6827c8b6fa234e4db0aa2188136c3c01.png)

```bash

## **数组转成字符串**

var array = ['apple', 'banana', 'orange'];
var separatedString = array.join('/');
console.log(separatedString);

字符串转数组
var string = 'apple/banana/orange';
var array = string.split('/');
console.log(array);

js  数组 json
var array = ['apple', 'banana', 'orange'];
var json = JSON.stringify(array);
console.log(json);

## **判断数组中存在为空的参数**

function areAllObjectPropertiesPresent(array) {
    
    
  return array.every(item => {
    
    
    const values = Object.values(item);
    return values.every(value => {
    
    
      if (typeof value === 'object' && value !== null) {
    
    
        // 判断对象类型的属性值
        return Object.values(value).every(propValue => propValue !== null && propValue !== undefined && propValue !== '');
      } else {
    
    
        // 判断其他类型的属性值
        return value !== null && value !== undefined && value !== '';
      }
    });
  });
}

// 示例用法
const data = [
  {
    
    
    "id": 9,
    "daily_id": 3,
    "staff_id": 12,
    "staff_name": "王老二",
    "profession_id": 3,
    "position_id": 3,
    "normal_time": "1",
    "overtime": "2",
    "member_bar": "杆件号001",
    "work_content": "方案1/测试/不锈钢/安装",
    "module_id": 2,
    "type": 1,
    "content": "使用内容1",
    "work_order_no": "工单编号1",
    "area": "施工区域1",
    "wbs_id": 25675,
    "staff_sn": "",
    "team_room": "队室-批量",
    "team_name": "班组-批量",
    "remiss_reason_id": 7,
    "unit": "计量单位-批量",
    "workload": "工作量-批量",
    "type_text": "返修",
    "profession_name": "瓦工",
    "position_name": "职务01",
    "module_name": "管线预制工厂化日报",
    "work_content1": ["方案1", "测试", "不锈钢", "安装"]
  },
  {
    
    
    "id": 10,
    "daily_id": 3,
    "staff_id": 17,
    "staff_name": "测试",
    "profession_id": 4,
    "position_id": 22,
    "normal_time": "3",
    "overtime": "4",
    "member_bar": "杆件号002",
    "work_content": "方案1/散件预制安装",
    "module_id": 3,
    "type": 2,
    "content": "使用内容2",
    "work_order_no": "工单编号2",
    "area": "施工区域2",
    "wbs_id": 25675,
    "staff_sn": "",
    "team_room": "队室-批量",
    "team_name": "班组-批量",
    "remiss_reason_id": 0,
    "unit": "计量单位-批量",
    "workload": "工作量-批量",
    "type_text": "追加",
    "profession_name": "电焊工",
    "position_name": "测试2",
    "module_name": "管线预制工厂化日报",
    "work_content1": ["方案1", "散件预制安装"]
  }
];

console.log(areAllObjectPropertiesPresent(data));  // 输出: true
## **js 判断数组中的某个 值不能为空**

function isNameEmpty(array, key) {
    
    
  for (let i = 0; i < array.length; i++) {
    
    
    if (!array[i][key] || array[i][key] === '') {
    
    
      return false; // 如果任何一个键值对为空,则返回 false
    }
  }
  return true; // 如果所有键值对都不为空,则返回 true
}

// 示例用法:
const array1 = [{
    
     name: 'John', age: 25 }, {
    
     name: 'Jane', age: 30 }, {
    
     name: '', age: 35 }];
console.log(isKeyValuePairEmpty(array1, 'name')); // 输出: false

const array2 = [{
    
     name: 'Alice', age: 28 }, {
    
     name: 'Bob', age: 32 }, {
    
     name: 'Charlie', age: 40 }];
console.log(isNameEmpty(array2, 'name')); // 输出: true
**

## 判断id相等-- 循环二维数组,判断二维数组下面id 与自定义id 相等取 file 文件地址

**
this.tabList = [{
    
    
	id: 0, // 分类id,设为0表示全部分类
	name: "全部", // 分类名称
	music: [],
}, {
    
    
	"id": 1, //分类id
	"name": "古典", //分类名称
	"create_time": "2023-11-03 15:01:36",
	"music": [{
    
    
		"id": 11,
		"name": "葬礼进行曲01(古典)", //音乐名称
		"file": "https://image.wxctj.com/20230602034042_16856916427503", //音乐地址
		"type_id": 1,
		"create_time": "2023-11-03 15:14:38"
	}]
},
{
    
    
	"id": 2, //分类id
	"name": "经典", //分类名称
	"create_time": "2023-11-03 15:01:36",
	"music": [{
    
    
		"id": 21,
		"name": "葬礼进行曲01(经典)", //音乐名称
		"file": "https://image.wxctj.com/20230602034042_16856916427503", //音乐地址
		"type_id": 1,
		"create_time": "2023-11-03 15:14:38"
	}, {
    
    
		"id": 22,
		"name": "葬礼进行曲02(经典)", //音乐名称
		"file": "https://image.wxctj.com/20230602034042_16856916427503", //音乐地址
		"type_id": 1,
		"create_time": "2023-11-03 15:14:38"
	}]
},
{
    
    
	"id": 3, //分类id
	"name": "现代", //分类名称
	"create_time": "2023-11-03 15:01:36",
	"music": [{
    
    
		"id": 31,
		"name": "葬礼进行曲01(现代)", //音乐名称
		"file": "https://image.wxctj.com/20230602034042_16856916427503", //音乐地址
		"type_id": 1,
		"create_time": "2023-11-03 15:14:38"
	}, {
    
    
		"id": 32,
		"name": "葬礼进行曲02(现代)", //音乐名称
		"file": "https://image.wxctj.com/20230602034042_16856916427503", //音乐地址
		"type_id": 1,
		"create_time": "2023-11-03 15:14:38"
	}]
},
{
    
    
	"id": 4, //分类id
	"name": "悲伤", //分类名称
	"create_time": "2023-11-03 15:01:36",
	"music": [{
    
    
		"id": 41,
		"name": "葬礼进行曲01(悲伤)", //音乐名称
		"file": "https://image.wxctj.com/20230602034042_16856916427503", //音乐地址
		"type_id": 1,
		"create_time": "2023-11-03 15:14:38"
	}, {
    
    
		"id": 42,
		"name": "葬礼进行曲02(悲伤)", //音乐名称
		"file": "https://image.wxctj.com/20230602034042_16856916427503", //音乐地址
		"type_id": 1,
		"create_time": "2023-11-03 15:14:38"
	}]
}
];

const musicId = 42; // 要匹配的音乐 ID
const matchedMusic = this.tabList.find(category => {
    
    
	return category.music.find(music => music.id === musicId);
});

if (matchedMusic) {
    
    
	const fileValue = matchedMusic.music.find(music => music.id === musicId).file;
	console.log(fileValue,'对应id 音乐文件');
} else {
    
    
	console.log("No matching music found.");
}
**

## 判断对象中所有值都不能为空

**
var objData = {
    
    
	longdu: res.data.longdu,
	longdu2: res.data.longdu2,
	longdu3: res.data.longdu3,
	latdu: res.data.latdu,
	latdu2: res.data.latdu2,
	latdu3: res.data.latdu3,
	longitudeType: res.data.longitudeType,
	latitudeType: res.data.latitudeType,
}

let hasEmptyValue = false;

for (let key in objData) {
    
    
	if (objData.hasOwnProperty(key)) {
    
    
		if (objData[key] == null || objData[key] == undefined || objData[key] == '' ||
			objData[key] == 'null') {
    
    
			hasEmptyValue = true;
			break;
		}
	}
}
if (hasEmptyValue) {
    
    
  // 存在一个或多个值为空
} else {
    
    
  // 所有值都不为空
}

Insert image description here

Guess you like

Origin blog.csdn.net/qq_38881495/article/details/134292823