Stephen
Stephen

Reputation: 1

filtering an array of timestamps

I'm working on a project that requires me to sort information by timestamp. All the information comes from a CSV file from a url. My plan is to 1)create an 1st array containing complete data 2)create a new Array of mapped timestamps 3)remove the duplicate timestamps 4)search the 1st Array using the new mapped and reduced Array.

The issue I think I'm having is that the Array contains the header "timestamp" at irregular intervals which I'm unable to filer out. This is what I'm doing.

  1. I am creating an array from a CSV file
const data = await csv({ delimiter: [";"] }).fromStream(request.get(fileUrl));

2)The data that I get has timestamps which I am then mapping

const timeStamps = data.map(ts => ts.timestamp)

the next step would be to remove the duplicate timestamps

enter image description here

function removeDuplicate(array, key) {
     var check = new Set();
     return array.filter(obj => !check.has(obj[key]) && check.add(obj[key]));

However when I try to run the above function it only returns the first timestamp. My thinking is that its running into the string 'timestamp and then stoping for some reason so I'm trying to filter out 'timestamp' from the array using

const filterdTimestamps = timeStamps.filter((ts) => ts.timestamp === 'timestamp');

Haven't been able to figure out what I'm doing wrong yet so any assistance would be greatly appreciated.

Upvotes: 0

Views: 235

Answers (1)

Brother58697
Brother58697

Reputation: 3178

You don't need to filter it. Create a set using the array, then spread it into a new array.

const input = ['5', '5', '5', '6', '6', '6', '7', '7','7','timestamp']
const output = [...new Set(input)]
console.log(output)

Upvotes: 2

Related Questions