I am working with angular4. I need to display a list with unique values.
When accessing an API I get an array, from that array I need to display only unique data. I will be accessing that api at certain time interval and I need to update list only if new data is present.
response= [{"id":"0DRDCH03DR51GGJGJNP80F7XZ8","value":"36af1784bec4_566601260"},{"id":"0DRDCFYGM2CAHAXYK96BPT9RHV","value":"36af1784bec4_566601140"},...]
listData = [];
for(let data of response) {
let tempValue = {id: '', time: ''};
let value = data.value.split('_')
if (value.length==2) {
if(value[value.length-1].length==2) {
tempValue.id = value[0];
tempValue.time = value[1];
}
let isPresent = false;
if(this.listData.length>0){
for(let value of this.listData){
if(value.time===tempValue.time){
isPresent = true;
}
}
}
if(!isPresent) {
this.listData.push(tempValue);
}
}
}
the final listData
listData = [{id:'36af1784bec4', time: '566601140'},...]
The above function does give me a unique listData array. I tried using array.filter and new Set but could not achieve the desired result.
I would like to know if there is an efficient way to do this.
idandvalue, then finding the existing of data may be more costly than updating the list regardless.Setand tried using.hasbut it always returnedtrueeven when thelistDatawas emptyidandvalue, but newlistDataisvalue.split('_')andvalue[0] is idandvalue[1] is time.listData = [{id: '36af1784bec4', time: '566601260'},...]