Using JavaScript to sort data within specific timeframes

How can I extract data based on timestamps greater than 06?

"use strict"

const data =  [ 
  {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224},
  {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436},
  {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804},
  {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941},
  {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049},
  {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846},
  {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298},
  {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267},
  {timestamp: "2020-04-23 05:45", totalAvg: 3.491275770565422},
  {timestamp: "2020-04-23 05:50", totalAvg: 3.9865817073170735},
  {timestamp: "2020-04-23 05:55", totalAvg: 2.8146341463414632},
  {timestamp: "2020-04-23 06:00", totalAvg: 4.0066161616161615},
  {timestamp: "2020-04-23 06:05", totalAvg: 4.870049261083743},
  {timestamp: "2020-04-23 06:10", totalAvg: 3.3189162561576357}
];
var filteredData = _.filter(data, item => {
  return !((parseInt(item.timestamp.substring(11, 13)) < 06) || (parseInt(item.timestamp.substring(11, 13)) < 24));
})

console.log(filteredData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

When attempting to utilize 06 in the script, an error is generated stating

Legacy octal literals are not allowed in strict mode

Answer №1

To easily find the substring containing the desired value, you can directly compare it as a string.

const data =  [ 
  {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224},
  {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436},
  {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804},
  {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941},
  {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049},
  {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846},
  {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298},
  {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267},
  {timestamp: "2020-04-23 05:45", totalAvg: 3.491275770565422},
  {timestamp: "2020-04-23 05:50", totalAvg: 3.9865817073170735},
  {timestamp: "2020-04-23 05:55", totalAvg: 2.8146341463414632},
  {timestamp: "2020-04-23 06:00", totalAvg: 4.0066161616161615},
  {timestamp: "2020-04-23 06:05", totalAvg: 4.870049261083743},
  {timestamp: "2020-04-23 06:10", totalAvg: 3.3189162561576357}
];
    
    
var filteredZoneData = _.filter(data, o => 
  o.timestamp.slice(11, 13) >= '06' && o.timestamp.slice(11, 13) < '24'
)

console.log(filteredZoneData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

Answer №2

When parsing integers, testing for 06 is unnecessary; therefore, modify it to:

var filteredZoneData = _.filter(
  data, o => +o.timestamp.substring(11, 13) >= 6 // there will be no 24 in the timestamp 
); 

Alternatively, for a range:

var filteredZoneData = _.filter(data, o => { 
  const t = +o.timestamp.substring(11, 13);
  return t >= 6 && t < 24;
}); 

Here's an example using lodash:

"use strict"

const data = [ {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224}, {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436}, {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804}, {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941}, {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049}, {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846}, {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298}, {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267}, {timestamp: "2020-04-23 05:45", totalAvg...
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

However, using a library may not be necessary as JavaScript can handle it on its own:

const data = [ {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224}, {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436}, {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804}, {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941}, {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049}, {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846}, {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298}, {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267}, {timestamp: "2020-04-23 05:45", tota...

let filteredZoneData = data.filter(
  dt => dt.timestamp.split(" ")[1] > "06"
);
console.log(filteredZoneData);

// or if you prefer

filteredZoneData = data.filter(
  dt => parseInt(dt.timestamp.split(" ")[1]) >= 6
);

console.log(filteredZoneData);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unlocking the secrets of capturing key presses before submitting with jQuery

I'm seeking help with creating an app that scans a barcode and displays the data on screen. I prefer not to use textboxes in order to prevent data editing. Currently, I have set up the enter key to be automatically sent at the end of the barcode scan ...

Beautiful prompt interface for HTML

I'm attempting to create a sweet alert using the HTML option: swal({ title: "HTML <small>Title</small>!", text: "A custom <span style="color:#F8BB86">html<span> message.", html: true }); Instead of just text, ...

Error arises when uploading csv files to Node.js with Multer due to an unexpected field presence

I'm currently working on implementing a file upload feature with the use of Node.js, Vue, and Multer. Below is the Vue.js front-end code: export default { data(){ return{ selected: "How do you want to input the data?", options: [ ...

Steps for displaying an error message when incorrect credentials are entered during a login attempt

As I work on implementing a login feature, I am facing the challenge of displaying an error message when users enter incorrect login credentials. My development environment involves using the POST method within Next.js. Here is a snippet of my code: ...

JavaScript and JSON interchangeably, the first AJAX response should be rewritten by the second response

I am facing an issue with my code: I have two ajax calls being made on window.load, and it seems like the response from the second AJAX call is overwriting the response from the first one before my function can process it. I'm not sure where I'm ...

AngularJS: The blend of bo-bind, bindonce, and the translate filter

I am currently working with angular 1.2.25, angular-translate 2.0.1, angular-translate-loader-static-files 2.0.0, and angular-bindonce 0.3.1. My goal is to translate a static key using bindonce. Here is the code snippet I have: <div bindonce> < ...

Inject data into an Angular 2 template

Does anybody know of a method to pass variables to templates in Angular2? Suppose I have the following code snippet: <div *ngFor="foo in foos"> <ng-container *ngTemplateOutlet="inner"</ng-container> </div> --------------- <n ...

Tips for choosing multiple values from a dropdown menu in Bootstrap CSS version 3

I'm looking to implement a way to select multiple values from a dropdown menu without using the CTRL key. Currently, I am utilizing Bootstrap CSS for styling. Here is the code for my dropdown: <select multiple class="dropdown-menu"> ...

Can webpack effectively operate in both the frontend and backend environments?

According to the information provided on their website, packaging is defined as: webpack serves as a module bundler with its main purpose being to bundle JavaScript files for usage in a browser. Additionally, it has the ability to transform, bundle, or ...

Deactivate the linear x axis labels in jQChart

I have a jQchart Linear chart that is displaying correctly and functioning properly. I am looking to remove or disable the X axis labels from the chart. ...

Determine the presence of a value within a specific column of an HTML table using jquery

When I input an ID number into a textbox, it shows me the corresponding location on a scale in another textbox. You can see an example of this functionality in action on this jsFiddle: http://jsfiddle.net/JoaoFelipePego/SdBBy/310/ If I enter a User ID num ...

You can't send headers to the client in Express after they have already been set

I successfully registered and inserted a record in my MongoDB. However, I encountered an error when trying to log in at the line "!user && res.status(401).json("Wrong User Name");" Cannot set headers after they are sent to the client at new NodeError ...

How can we access a value within a deeply nested JSON object in Node.js when the key values in between are not

In the nested configuration object provided below, I am seeking to retrieve the value associated with key1, which in this case is "value1". It's important to note that while key1 remains static, the values for randomGeneratedNumber and randomGenerated ...

Determine distinct items in an array that match a predefined criteria

I have a list of objects with two keys, img1 and img2. I need to identify unique objects based on the values of img1, while also retaining the corresponding value of img2. This is the current code I am using: const imgs_arr = [ ...new Set( inpu ...

Implementation of Material UI Autocomplete feature with options consisting of an array of objects linking to both ID and label properties

Utilizing the Material UI Autocomplete component in my project has been a key feature. Referencing the official documentation, here are the available options: let options = [ { id: "507f191e810c19729de860ea", label: "London" }, { id: "u07f1u1e810c19 ...

Having issues with angular-file-upload failing to include the file in the request

I am currently using angular-file-upload in order to upload an image to a server that is utilizing Node and Express 4.0. However, I am encountering difficulties in accessing the file data on the server. Below is the relevant code snippet: <input type= ...

Extract all links from an external website

I'm attempting to extract all the URLs from a webpage using jQuery so that I can later use them with $.get(). If these URLs were located on the same page as the script, retrieving them would be easy by doing something like var links = document.getEle ...

Contemplating the Sequence of DOM Execution

In the world of html/javascript, the order of execution is typically serial, meaning that the browser reads the code line by line and interprets it accordingly. This is a common practice in most programming languages. Some javascript programmers prefer to ...

Angular Material (8) error code S2591: The variable 'require' is not defined in the current scope

Currently, I am attempting to record the date and time in the JavaScript console. Despite the code successfully logging the dates, an error message persists: Note: The code is functioning properly, with the dates being displayed in the console. It is only ...

Tips for conducting a simultaneous test on several devices

Struggling to execute a single test script on multiple devices, regardless of efforts. There is one test apk and script obtained from a website as a sample. The script locates a textbox in the application, enters "Hello World!" into it, then completes the ...