Using JLinq for Date-Based Filtering of JSON Data

I have been trying to use JLinq for filtering JSON data, but I am facing an issue with filtering by date. Despite attempting different methods, the problem persists.

Below is a snippet of my code:

//This works
jlinq.from(myData)
         .select(function(rec) {
              return {
                   title: rec.title,
                   pubDate: rec.pubDate
               }
          });

This is the data returned by the above code:

// And returned this data
myData = [
    {pubDate: "2012-06-19T10:42:59.000Z",
    title: "How caching matters in JS?" },
    {pubDate: "2012-06-18T14:39:25.000Z",
    title: "Globbing in scripting languages"},
    {pubDate: "2012-06-14T17:50:22.000Z",
    title: "Inspecting Objects in Scripting"}
]

When I attempt to filter by date (greater than or less than), it does not work as expected.

EDITED

//Filter by pubDate
jlinq.from(myData)
           .greater(new Date("pubDate"), new Date("2010-02-13T19:03:17.000Z"))
           .select();

This issue seems to arise specifically when comparing Date values, resulting in an empty array being returned.

//But Returns empty Array
//[]

Answer №1

I have figured it out. The issue seemed to be that the library was assuming that if the value is a string, it should compare based on length, otherwise just compare value1 > value2.

I made the following change to the code:

    //is the value greater than the argument
    { name:"greater", type:framework.command.query, 
        method:function(value) {
            return this.compare({
                array:function() { return this.value.length > value; },
                string:function() { return this.value.length > value; },
                other:function() { return this.value > value; }
            });
        }},

Instead, I created another filter called greaterDate and converted the string to 'Date()'

    //is the value greaterDate than the argument
    { name:"greaterDate", type:framework.command.query, 
        method:function(value) {
            return this.compare({
                array:function() { return this.value.length > value; },
                string:function() { return new Date(this.value) > new Date(value); },
                other:function() {  return new Date(this.value) > new Date(value); }
            });
        }},

There may be a more efficient solution, but this workaround has resolved the issue for now.

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

Guide on accessing JSON field through GSON library with @SerializedName annotation

I am encountering an issue with parsing the JSON object provided below: "paymentCurrency": "eur", "paymentOptions": [ { "paymentOptionId": "1CeGuJt2nkxmaMVf", ...

The subscriber continues to run repeatedly, even after we have removed its subscription

The file brief-component.ts contains the following code where the drawer service is being called: this.assignDrawerService.showDrawer(parameter); In the file drawer-service.ts: private drawerSubject = new BehaviorSubject<boolean>(false); public ...

Utilizing Spring's Rest Service in Version 4.x

Despite encountering numerous inquiries on the subject, none have proven helpful to me. I'm attempting to launch a basic REST service, but no matter what I try, it just won't work. My initial approach was to set up a REST controller like this: ...

Implementing Google AdWords Conversion Tracking code on a button click event using knockoutjs

I recently received the following code snippet from Google AdWords for tracking purposes. <script type="text/javascript"> /* <![CDATA[ */ var google_conversion_id = 973348620; var google_conversion_language = "en"; var ...

Issue encountered when attempting to export dictionary data to a JSON file using the cx_Oracle LOB object

I'm facing an issue with my select query, which is executed using the cx_oracle library. The result of this query is stored as a list of dictionaries and I need to save it in a json file for future use and iterations. However, the output contains a " ...

Consider creating a distinct document for your "scripts"

Within my package.json configuration file, the "scripts" section contains numerous commands structured as shown below. "scripts" { "script1": "command example", "script2": "command example", "script3": "command example", "script4": "command exampl ...

"Utilizing Bootstrap Tour for Easy Navigation: Automatically Scroll Back to the Top with

I have a Bootstrap tour set up with code that is meant to capture the user pressing the end tour button and scroll them back to the top of the screen. However, currently the code runs when a new popover loads instead of when the end tour button is clicke ...

Tips for testing Sequelize with Jasmine

In my database/index.ts file, I set up a Sequelize database using the code below: import { Sequelize } from 'sequelize'; const { DATABASE_DIALECT, DATABASE_HOST, DATABASE_PORT, DATABASE_USER_NAME, DATABASE_USER_PASSWORD, DATABASE_NAM ...

Vue.js SyntaxError: Identifier came out of nowhere

An error was reported by VUE debug mode in this line of my code: :style="{transform : 'translate3d(' + translateX + 'px,0, 0)'}"\ The documentation does not provide instructions on how to include a variable within a style binding ...

The `Route` component is expecting a `function` for the `component` prop, but instead it received an `object`

For a while now, I've been grappling with an issue that seems to be unique to me. The problem lies within my component and container setup for the start screen rendering at the initial route. components/DifficultySelection.jsx import React from &apo ...

AngularJS is not showing the dropdown options as expected

I am facing an issue where the dropdown list options are not displaying, even though I am able to fetch the data in the controller but not in HTML. Here is the code snippet: In HTML <select name="nameSelect" id="nameSelect" ng-model="model.name"> ...

Can a TypeScript file be created by combining a declaration file and a .js file?

It is commonly understood that declaration files are typically used for libraries rather than projects. However, let's consider a scenario where an existing JavaScript project needs to be migrated to TypeScript by creating d.ts files for each source ...

Using Javascript to refresh an image and update a class within a DIV when another DIV is clicked

I have a code that is quite lengthy to share here, so I've created a demo on JSFiddle: http://jsfiddle.net/Emily92/5b72k225/ This particular code takes a random image and divides it into multiple sections based on the class applied to the div contain ...

Lighthouse Issue: Facing PWA Challenges with a "Request Blocked by DevTools" Error

For hours now, I've been struggling to make Lighthouse work in Chrome for my initial PWA project. I feel completely lost as nothing seems to be making sense despite the basic code I have included below. The issue arises when I load the page normally ...

Firefox not clearing Highcharts points properly

I am currently utilizing highcharts to generate dynamic charts when hovering over a table row. My goal is to clear and hide the chart once the mouse moves away from the row. While this functionality works smoothly in Chrome, I am encountering a strange is ...

Struggling with flipping div functionality in IE9 on flipping content demo

I recently developed a flipping div using transitions and 3D transforms based on the Flipping Content Demo 1 from . While this works smoothly on most browsers, unfortunately IE 9 does not support transitions and 3D transforms. In order to address this iss ...

Vanish Dropdown upon clicking Using JavaScript

Hey, I'm new to web development and I'm currently working on creating a web app for iPhone. Everything is going smoothly except for one issue - my dropdown menu works perfectly on desktop Chrome, but on the iPhone's Safari browser, I can&ap ...

Customizing listview appearance in asp.net with css and enhancing with javascript

When I print, the css class is not being used although it appears when using the program <script type="text/javascript"> function printDiv() { var divToPrint = document.getElementById('DivIdToPrint'); ...

What is the best way to convert a Date into the JSON format before sending it to the database?

I'm currently delving into backend development with Node.js, and I am in the process of connecting my backend to a MongoDB. Specifically, I am working on creating a User object that includes Birth Date as one of its properties. However, I am strugglin ...

How to use ngModel directive in Angular to select/unselect dynamically generated checkboxes and retrieve their values

Currently, I am working with a dataset retrieved from an API and dynamically creating checkboxes in my HTML page using the DataView component from PrimeNG. My objective is to implement a feature where users can select or deselect all checkboxes with a cli ...