I am trying to remove duplicates from data passed on from the Node. The function to fetch the data is:
getProducts(_) {
const dev = "http://localhost:3008/api"
fetch(dev)
.then(res => res.json())
.then(data => {
/* this.setState
({
data: data.data
})
})*/
this.removeDuplicates(data)
})
}
My remove duplicates function is:
removeDuplicates(arr){
let array = arr.data
console.log(array)
array.sort((a,b) => {return a.webservice - b.webservice})
let length = array.length
for ( var i = 0;
i < length;
i++ ){
let x = i+1
console.log(array[x].webservice)
/* if (array[i].webservice == array[x].webservice){
delete array[i]
}*/
// console.log(array)
}
}
When I console.log the data
- it returns the array of object as expected.
When I console.log the array[i].webservice
- it returns the webservice
But when I try to do the same for array[x].webservice
(which is i+1) it returns array[x] is undefined fatal error but still console.logs the data.
Am I missing something here?