0

Given the following data structure:

[{
  start: 10.2,
  end: 15.3,
  text: 'Lorem Ipsum....'
}, {
  start: 16.3,
  end: 20.5,
  text: 'dolor sit...'
}, {
  start: 19.4,
  end: 25.1,
  text: 'consectetur adipiscing elit'
}]

I need to figure out the active text for a given time (e.g. 18, which maches element 2 and 3). I already implemented a find method that checks if the given time is greater than start and lesser than end. Unfortunately that's not as efficient for around 25k entries. I'm therefore looking for a hashmap implementation that has calculates start and end as keys and returns for a given time the matching entries.

2 Answers2

0

it will helpful to you to filter the data

var arr=[{
  start: 10.2,
  end: 15.3,
  text: 'Lorem Ipsum....'
}, {
  start: 16.3,
  end: 20.5,
  text: 'dolor sit...'
}, {
  start: 19.4,
  end: 25.1,
  text: 'consectetur adipiscing elit'
}]

var condition=(arr)=>(arr.start>=18);

var arr_1=arr.filter(condition);
console.log(arr_1);
Kalaiselvan
  • 2,095
  • 1
  • 18
  • 31
0

For sorted data, you could take a binary seach where the start is in the middle of the list and if the wanted date is smaller, take the middle of start and middle or the middle values of the other side. Proceed until the item is found.


For unsorted data, you could take the year as part of the hash key and the week number for every event in the list.

If an event is ends in another week, add all references of the event in the hash table with all the weeks where it takes place.

For a seach, just calculate the week number and take a fine seach on the given week data.

Instead of the week, you could take a shorter range, like a day or a greater range, like the first half of the month or the last half of the month (this would be easier to calculate then a week).

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392