当前位置:网站首页>Object array de encapsulation
Object array de encapsulation
2022-06-13 08:41:00 【Python's path to becoming a God】
/**
* Array object de duplication
* @param {Array} arr An array of objects that need to be de duplicated
* @param {string} key Identify duplicate keywords , If basic data type , No transmission
* */
function arrNoRepeat(arr, key) {
if(!key){
return [...new Set(arr)]
}
let obj = {}
arr = arr.reduce((item, next) => {
obj[next[key]] ? '' : obj[next[key]] = true && item.push(next)
return item
}, [])
return arr
}
Test code ( The basic data type can also )
/**
* Array object de duplication
* @param {Array} arr An array of objects that need to be de duplicated
* @param {string} arr Identify duplicate keywords , If basic data type , No transmission
* */
function arrNoRepeat(arr, key) {
if(!key){
return [...new Set(arr)]
}
let obj = {}
arr = arr.reduce((item, next) => {
obj[next[key]] ? '' : obj[next[key]] = true && item.push(next)
return item
}, [])
return arr
}
// Arrays that need to be de duplicated
let myArr = [
{
id: 1,
name: ' Xiao Ming '
}, {
id: 2,
name: ' Xiaohong '
}, {
id: 1,
name: ' Xiao Ming '
},
]
console.log(arrNoRepeat(myArr, 'id'))
console.log(arrNoRepeat(myArr, 'name'))
let arr = [1,2,3,4,1]
console.log(arrNoRepeat(arr))
边栏推荐
- Logstash failed to create queue
- Time and date processing in JS
- Is there any good management software to solve the problems faced by tea wholesalers
- HCIP_ Static experiment
- 情绪处理技巧
- Custom exception class myexception
- Mysql_ Preliminary summary of database data (Continued)
- Buuctf web (V)
- JS - max. of array cases
- Namespace in TS (1)
猜你喜欢
WARNING:tornado. access:404 GET /favicon. ICO (172.16.8.1) 1.84ms [with static file settings]
Buuctf web (VI)
Is signed or unsigned selected to create an integer field in MySQL? The answer is as follows:
Use of addeventlistener in JS
MySQL queries difference sets (missing data) by linking tables based on an associated field
Container concept and cloud native
VI editor
Bidirectional retransmission step experiment
JS - max. of array cases
DHCP principle and configuration
随机推荐
Shellshock Attack Lab
7、 JS data type
Use of addeventlistener in JS
Logstash failed to create queue
Mobile terminal development I: basic concepts
JS - print 99 multiplication table of the for cycle case
JS - for cycle case: Horse grain
Batch package and download Alibaba OSS files
1.初步认识express
About redis encapsulation tool class using distributed locks
Is there any good management software to solve the problems faced by tea wholesalers
Dest0g3 520迎新赛
Container concept and cloud native
JS array using the reduce() method
Remote access and control
Review one flex knowledge point every day
抖音关键词搜索列表接口,超详细的接口对接步骤
Notes on development experience: TP5 exp query, SQL analysis, JQ, applet, right-click menu, Linux skills, shell skills, mysql, etc
File upload question type
MySQL queries difference sets (missing data) by linking tables based on an associated field