A friend of mine sent me a message the other day saying, “Are array objects reweighted by the properties specified in the object? Let me write. “WHEN I saw this, I was a little confused. I didn’t know.
I can only bite the bullet and die, almost from the entry to give up, in the friend step by step guidance finally write good, friends summed up sent me a copy, in the spirit of voluntary sharing. I forgot about it for the last two days
I happened to come across this thing in the project. At that time, I had only a little thought left in my mind, but when I tried to recall it, my memory was already vague. Very helpless ah, how to do? Project development needs, brave to find a friend for a copy of ha ha. Now I decided that I want to build a small js warehouse of my own, which I do not understand, something has nothing to turn over, wen reason
I like to communicate with my friends. Occasionally, I will ask some technical questions to me, but I can’t do that. Under the guidance of each time, I gradually understand and learn new knowledge. Haha learn and progress together, welcome technical exchanges
Question: Array objects are de-duplicated according to the properties specified in the object?
Method 1: Reduce method
function unique(arr,u_key){
let obj = {}
arr.reduce((prev,next)=>{
obj[next[u_key]+typeof next[u_key]] ? ' ' :
obj[next[u_key]+typeof next[u_key]] = true && prev.push(next)
return prev
},[])
}
Copy the code
The push method returns the length of the new array, and the && method returns the following value, but we need an array object that is executed for the first time, so we write another line return prev
Method two: counter principle
function unique(arr,u_key){
letResult = [] result[0] = arr[0] arr.foreach ((meta_item, I)=>{// Declare count variables, push if an object in the source array is different from all objects in the result arraylet num = 0
result.forEach((r_item,j)=>{
if(meta_item[u_key]! ==r_item[u_key]) { num++ }if (num === result.length) {
result.push(meta_item)
}
})
})
return result
}
Copy the code
Method 3: Simple rough loop, using the principle that the object of the same name will be overwritten (similar to the first method)
function unique(arr,u_key) {
let obj = {}
let result = []
arr.forEach(item=>{
let typeof_key = typeof item[u_key] + item[u_key]
obj[typeof_key] = item
})
for (let key in obj) {
result.push(obj[key])
}
return result
}
Copy the code
Method 4: ES6 Map
function unique(arr,u_key) {
let map = new Map()
arr.forEach((item,index)=>{
if(! map.has(item[u_key])){ map.set(item[u_key],item) } })return [...map.values()]
}
Copy the code
This method is the answer given by my Java colleagues after watching me dig gold, maybe this is the big guy, for ES6 map, set, I only know there is this thing, but I have not in-depth understanding. To my shame, Java plays ES6 better than I do, and I’m going to dive into the sea of learning and reach its high test case
let arrayList = [{
id:'1',
name:'one'
},{
id:'2',
name:'tow',
},{
id:'3',
name:'three'
},{
id:'1',
name:'one'
},{
id:2,
name:'tow',
},{
id:'3',
name:'three'
}]
unique(arrayList,'id')
Copy the code
If there is something wrong, I hope I can point it out. If there is a better way, I hope I can learn to communicate
This article addresses
My blog is synchronized to tencent cloud + community, invite everyone to come together: cloud.tencent.com/developer/s…