当前位置:网站首页>JS array de duplication (including simple array de duplication and object array de duplication)
JS array de duplication (including simple array de duplication and object array de duplication)
2022-07-27 04:03:00 【Chaoyang 39】
Simple array de duplication
Simple array : An array of basic data type elements , Such as [1,‘ Hello ’,true]
Mode one : utilize Set duplicate removal
utilize Set The feature implementation of automatic de duplication of data structures
let oldList = [1, 2, 3, 3];
let newList = Array.from(new Set(oldList)); // obtain [1, 2, 3]
Similar writing is as follows :
function arrDistinct(arr){
const newArr = new Set(arr);
return [...newArr]
}
let list = [1,2,2,3,4,4]
console.log(arrDistinct(list))
Mode two : Nested loop , Compare yourself with yourself , If there is any repetition, delete
let arr = [1, 2, 3, 2, 2, 1, 3, 4, 2, 5];
// Remove duplicate numbers from the array
for (let i = 0; i < arr.length; i++) {
/* Get all elements after the current element */
for (let j = i + 1; j < arr.length; j++) {
// Determine whether the values of two elements are equal
if (arr[i] == arr[j]) {
// If equal, it proves that there are duplicate elements , Delete j Corresponding elements
arr.splice(j, 1);
// When you delete the current j After the element , The following elements will be filled automatically
// send j Self reduction , Compare again j The element of the location
j--;
}
}
}
console.log(arr); // The result is [ 1, 2, 3, 4, 5 ]
Mode three : Put the non repeating elements into the new array
// Ideas : Create a new array , Loop traversal , As long as the new array has the value of the old array , There's no need to add .
function arrDistinct(array) {
let newArr = [];
for (let i = 0; i < array.length; i++) {
// Opening and closing principle
let bool = true;
// Every time you have to judge whether there are values in the old array in the new array .
for (let j = 0; j < newArr.length; j++) {
if (array[i] === newArr[j]) {
bool = false;
}
}
if (bool) {
newArr[newArr.length] = array[i];
}
}
return newArr;
}
let oldList = [1, 2, 3, 4, 5, 2, 3, 4];
let newList = arrDistinct(oldList);
console.log(newList); // result [ 1, 2, 3, 4, 5 ]
Mode 4 : Use lodash Of uniq()
_.uniq([2, 1, 2]); // => [2, 1]
Object array de duplication
An array of objects : An array of objects
function arrDistinctByProp(arr,prop){
// Return only the first occurrence of , Filter out the repeated ones
return arr.filter(function(item,index,self){
return self.findIndex(el=>el[prop]==item[prop])===index
})
}
let list = [
{
key:'1',
name:' Brigitte Lin '
},
{
key:'2',
name:' Zhang Sanfeng '
},
{
key:'1',
name:' Duan Yu '
},
{
key:'1',
name:' Duan Yu '
}
]
// adopt name Attribute de duplication
console.log(arrDistinctByProp(list,'name'))
Another possibility is easier to understand , It is written as follows :
function soloArray(array, UID) {
let newArr = [];
let UIDlist = [];
array.forEach((item) => {
if (!UIDlist.includes(item[UID])) {
UIDlist.push(item[UID]);
newArr.push(item);
}
});
return newArr;
}
let oldList = [
{
ID: 1,
name: " X.h. ",
age: 10,
},
{
ID: 2,
name: " Zhang San ",
age: 20,
},
{
ID: 2,
name: " Zhang San ",
age: 20,
},
{
ID: 3,
name: " Li Si ",
age: 30,
},
];
let newList = soloArray(oldList, "ID");
console.log(newList);
边栏推荐
猜你喜欢
随机推荐
Day 28 of leetcode
电商系统结合商品秒杀活动,VR全景不断带来收益
Machine learning [Matplotlib]
03. Get the web page source code
unity之二维数组实现正六边形地图
分析一下CSDN大佬写的CAS,可重入锁, 非公平锁
04.在谷歌浏览器中安装模拟浏览器ChromeDriver的详细步骤
mysql中case when返回多个字段处理方案
路由策略第一关
线上一个隐匿 Bug 的复盘
C. Cypher
STM32CubeMX学习笔记(41)——ETH接口+LwIP协议栈使用(DHCP)
222. 完全二叉树的节点个数
Two help points distribution brings to merchants
Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
11.zuul路由网关
小于等于K的最大子数组累加和
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣
LeetCode 第二十八天









