I'm currently working with angular4 and facing a challenge of displaying a list containing only unique values.
Whenever I access an API, it returns an array from which I have to filter out repeated data. The API will be accessed periodically, and the list should be updated only if new data is available.
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 resulting list:
listData = [{id:'36af1784bec4', time: '566601140'},...]
The above code provides me with a listData array containing unique values. Despite attempting methods like array.filter and new Set, I couldn't achieve the desired outcome.
If you have any suggestions on how to improve the efficiency of this process, please share them with me.