js数组中两个数组集合【每个元素都是集合】,取数组1与数组2不同的值

js数组中两个数组集合【每个元素都是集合】,取数组1与数组2不同的值

js数组中两个数组集合【每个元素都是集合】,取数组1与数组2不同的值_第1张图片

前提:存在数组A、数组B

1.数组自身去重【A+B的数组集合自身去重】

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<button type="button" onclick="decode()" style="width: 50px;height: 70px;"></button>
		<script type="text/javascript">
			let arr1 = [{
				"code": "0001",
				"name": "liting",
				"age": 12,
				"school": "宁城一中",
			}, {
				"code": "0002",
				"name": "张三",
				"age": 13,
				"school": "宁城一中",
			}, {
				"code": "0003",
				"name": "晓明",
				"age": 14,
				"school": "宁城一中",
			}, {
				"code": "0006",
				"name": "花花",
				"age": 17,
				"school": "济阳一中",
			},{
				"code": "0007",
				"name": "凯凯",
				"age": 17,
				"school": "济阳一中",
			},{
				"code": "0008",
				"name": "明明",
				"age": 17,
				"school": "济阳一中",
			}]
			let arr2 = [{
				"code": "0001",
				"name": "liting",
				"age": 12,
				"school": "宁城一中",
			}, {
				"code": "0002",
				"name": "张三",
				"age": 13,
				"school": "宁城一中",
			}, {
				"code": "0003",
				"name": "晓明",
				"age": 14,
				"school": "宁城一中",
			}, 
			{
				"code": "0004",
				"name": "芳芳",
				"age": 15,
				"school": "宁城一中",
			},{
				"code": "0005",
				"name": "红红",
				"age": 16,
				"school": "宁城一中",
			},
			]
			console.log(arr1, "arr1");
			console.log(arr2, "arr2");
			// 自身去重
			let conArr = arr1.concat(arr2)
			function unique(arr) {
				let newArr = [arr[0]]
				for (let i = 1; i < arr.length; i++) {
					let repeat = false;
					for (let j = 0; j < newArr.length; j++) {
						console.log(arr[i], "arr[i]");
						console.log(newArr[j], "newArr[j]")
						if (arr[i].code === newArr[j].code) {
							repeat = true;
							break;
						} else {
						}
					}
					if (!repeat) {
						newArr.push(arr[i]);
					}
				}
				return newArr;
			}
			console.log(unique(conArr), "conArr")
		</script>
	</body>
</html>

2.两个数组去重【A-B】|| 【B-A】

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<button type="button" onclick="decode()" style="width: 50px;height: 70px;"></button>
		<script type="text/javascript">
			let arr1 = [{
				"code": "0001",
				"name": "liting",
				"age": 12,
				"school": "宁城一中",
			}, {
				"code": "0002",
				"name": "张三",
				"age": 13,
				"school": "宁城一中",
			}, {
				"code": "0003",
				"name": "晓明",
				"age": 14,
				"school": "宁城一中",
			}, {
				"code": "0006",
				"name": "花花",
				"age": 17,
				"school": "济阳一中",
			},{
				"code": "0007",
				"name": "凯凯",
				"age": 17,
				"school": "济阳一中",
			},{
				"code": "0008",
				"name": "明明",
				"age": 17,
				"school": "济阳一中",
			}]
			let arr2 = [{
				"code": "0001",
				"name": "liting",
				"age": 12,
				"school": "宁城一中",
			}, {
				"code": "0002",
				"name": "张三",
				"age": 13,
				"school": "宁城一中",
			}, {
				"code": "0003",
				"name": "晓明",
				"age": 14,
				"school": "宁城一中",
			}, 
			{
				"code": "0004",
				"name": "芳芳",
				"age": 15,
				"school": "宁城一中",
			},{
				"code": "0005",
				"name": "红红",
				"age": 16,
				"school": "宁城一中",
			},
			]
			console.log(arr1, "arr1");
			console.log(arr2, "arr2");			
			// 比较arr与arr1的不同【arr有,arr1没有】
            let repeat = null
			// 两个数组去重
			function unique(arr,arr1) {
				let newArr = []
				for (let i = 0; i < arr.length; i++) {
					repeat = false
					for (let j = 0; j < arr1.length; j++) {
						//比较条件,唯一值相等
						if (arr[i].code === arr1[j].code) {
							repeat = true;
							break;
						}
					}
					if (!repeat) {
						newArr.push(arr[i]);
					}
				}			
				return newArr;
			}
			//arr1与arr2【找arr1中存在,arr2中不存在】
			let uniArr = unique(arr1,arr2)
			console.log(uniArr,"去重")
			//arr1与arr2【找arr2中存在,arr1中不存在】
			let uniArr1 = unique(arr2,arr1)
			console.log(uniArr1,"去重1")
		</script>
	</body>
</html>

你可能感兴趣的:(前端学习-原创,js,数组)