1. Array deduplication/************************************************** ╚description: ╚Author: Qilin Society╚Time: 2021-09-13 22:26:21 ╚Name: V1.0.5 ***************************************************/ var obj = ['Qilin','She','CC','DD','Qilin','She','11',11] //Define a new array var s = []; //Traverse the array for(var i=0;i<obj.length;i++){ if(s.indexOf(obj[i]) == -1){ //Judge whether it exists in the s array, if not, push it into the s array s.push(obj[i]); } } console.log(s); 2. Deduplication of objects in arrays/************************************************** ╚description: ╚Author: Qilin Society╚Time: 2021-09-13 22:26:21 ╚Name: V1.0.5 ***************************************************/ var old_data = [ { name:'ccc', age:'18' }, { name:'peng', age:'18' }, //Remove duplicate peng { name:'aaa', age:'18' }, { name:'peng', age:'18' }, ] // Method 1: Use the object access attribute method to determine whether the key exists in the object var result = []; var obj = {}; old_data.forEach(function (data) { if(!obj[data.name]){ result.push(data); obj[data.name] = true; } }) console.log(result); 3. Modify the value of another field based on the same field in the array/************************************************** ╚description: ╚Author: Qilin Society╚Time: 2021-09-13 22:26:21 ╚Name: V1.0.5 ***************************************************/ var oldData = [ { name:'cccc', age:'5656' }, { name:'cccc', age:'22dddsada' }, { name:'cccc', age:'22dddsada' }, { name:'aaaa', age:'32' }, { name:'aaaa', age:'2dasdasdas2' }, ] var newArr = []; for (var i = 0; i < oldData.length; i++) { var item = oldData[i]; var isExists = false; for (var j = 0; j < newArr.length; j++) { var item2 = newArr[j]; if (item2.name == item.name) { isExists = true; break; } } if (isExists) { // Find the same here, change the same if(item.name == 'cccc'){ item.age = '222222' item2.age = '222222' }else{ item.age = '3333' item2.age = '3333' } newArr.push(item2); continue; } newArr.push(item); } console.log(newArr) SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Basic syntax of MySQL index
>>: How to switch directories efficiently in Linux
These two attributes are often used, but their di...
Introduction to the polling algorithm Many people...
Document mode has the following two functions: 1. ...
In fact, it is very simple to encapsulate axios i...
This article example shares the specific code of ...
Table of contents Before MySQL 5.6 After MySQL 5....
Table of contents Transaction Isolation Level Wha...
Unique “About”-Pages A great way to distinguish yo...
I had been using MySQL 5.7 before, but because My...
Table of contents docker system df docker system ...
1. Introduction: If we want to display flash conte...
If you have a choice, you should use UTF-8 In fac...
My machine environment: Windows 2008 R2 MySQL 5.6...
The div element is used to provide structure and b...
In the previous article, I introduced how to solv...