Tips for creating an Enumerable 'similarity' search using linq.js

I have a JSON array that looks like this:


[
  {
    "_Id": "0001",
    "_PatentId": "0000",
    "_Text": "Employee",
    "_Value": "employee",
    "_PermissionLevel": 55      
  },
  {
    "_Id": "0002",
    "_PatentId": "0000",
    "_Text": "Employees",
    "_Value": "employees",
    "_PermissionLevel": 55
  },
 {
    "_Id": "0002",
    "_PatentId": "0001",
    "_Text": "Dept",
    "_Value": "Dept",
    "_PermissionLevel": 55
  }
]

With this JSON array, I am trying to filter employees using the like operator. The query I used below is working as expected.

var qryResult = Enumerable.From(_gramrTree).Where("$._Text == 'Employee'").OrderBy("$._Id").Select("$._Id").ToArray();

However, I also need to use the like operator but it's not giving the desired outcome.

Unsuccessful queries

var qryResult = Enumerable.From(_gramrTree).Where("$._Text like '%Emp%'").OrderBy("$._Id").Select("$._Id").ToArray();

var qryResult = Enumerable.From(_gramrTree).Where("$._Text % 'Emp'").OrderBy("$._Id").Select("$._Id").ToArray();

Answer №1

Here is a workaround that may work for you (it appears the 'like' function is not included)

.Where("~($._Text).toUpperCase().indexOf('emp'.toUpperCase())")

Take a look at this working example:

var _gramrTree = [{ "_Id": "0001", "_PatentId": "0000", "_Text": "Employee", "_Value": "employee", "_PermissionLevel": 55 }, { "_Id": "0002", "_PatentId": "0000", "_Text": "Employees", "_Value": "employees", "_PermissionLevel": 55 }, { "_Id": "0002", "_PatentId": "0001", "_Text": "Dept", "_Value": "Dept", "_PermissionLevel": 55 }],
    qryResult = Enumerable.From(_gramrTree).Where("~($._Text).toUpperCase().indexOf('emp'.toUpperCase())").ToArray();

document.write('<pre>' + JSON.stringify(qryResult, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

Answer №2

I discovered another method as well..

var results = Enumerable.From(_grammarTree)
    .Where("!!$._Text.match(/^"Emp"/i)")
    .OrderBy("$._Text")
    .Select("$._Text")
    .ToArray()

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

New from Firefox 89: The afterprint event!

Having an issue with this fragment of code: const afterPrint = () => { this.location.back(); window.removeEventListener('afterprint', afterPrint); }; window.addEventListener('afterprint', afterPrint); window.print(); I&apos ...

Turn off integrity verification for local dependencies in package-lock.json

Is there a way to bypass the integrity check for a local dependency in package-lock.json? Within my project repository, I have a core library along with two Angular applications that both rely on this core library as a dependency. The problem arises beca ...

Utilize Angular to implement tickbox filtering on a JSON file

Is there a way to filter JSON results based on both ladies and mens 'styles' without needing three tick boxes? Some products have dual gender styles, so how can we display results for 'both' without duplication? Your help is much apprec ...

Highcharts - resolving cross-browser e.Offset discrepancies in mouse event detection on charts

I need to determine if the mouseup event is inside the chart and display the coordinates of the point. The code works in Chrome but not in Firefox due to the lack of the event.offset property. jQuery(chart.container).mouseup(function (event) { eoff ...

When converting a double value to a long type in GWT, an incorrect number may

I'm not sure if I've encountered a bug, reached a limitation of GWT, or simply made an error by creating a double with such a high number. When performing the following code on the client side: Double d = 10152826455075087d; GWT.log("Value is n ...

The `this` value is null within an onClick function of a ReactJS component

The issue occurs with the onClick method of <span className="nav-toggle">. The specific error message is: cannot read property setState of null. It seems to be related to the scoping of this or due to the asynchronous nature of the setState method. ...

The coordinates of the event do not match the coordinates of the location. Successful AJAX response data

How can I retrieve the accurate latitude and longitude when the Google Maps marker finishes dragging? It's confusing because for the same exact point, two different (but very approximate) coordinates are provided. Here are some example results for t ...

Discover a multitude of items simultaneously using mongoose

I am trying to find multiple objects in a mongo model with different properties simultaneously. model.find({uuid: 235q422462}, {uuid: 435q4235239}, function(err, objects){ if(err){ console.log(err) } else { console.log(objects) ...

The Chrome (version 58) webdriverio is currently inactive, while Firefox is up and running

Previously, I successfully ran automation tests on Firefox and Chrome locally. However, there seems to be an issue that has arisen when trying to run them on Chrome recently. My system configurations: Operating System: Windows 10 (64-bit) Chrome Versio ...

Experimenting with the inner workings of a method by utilizing vue-test-utils alongside Jest

Is there a way to properly test the internal logic of this method? For instance: async method () { this.isLoading = true; await this.GET_OFFERS(); this.isLoading = false; this.router.push("/somewhere"); } This method toggles isLoading, ...

Dropzone.js: Creating a personalized file explorer to include files that have already been uploaded

Don't worry, this isn't your typical "can't load files from the server" query... I'm looking to allow users to view files on the server in a bootstrap modal and then select specific files. After selection, I want to close the modal and ...

The script is showcasing text on both instances

I am currently working on a script to show text to users who are using adblock. The script I am using is as follows: ads.js <script>var canRunAds = true;</script> index.php <script data-rocketsrc="ads.js" type="text/rocketscript">< ...

Encountering cross-domain issues while integrating AngularJS in flex (web control), and trying to retrieve templates/json (local files)

To provide some background on my current endeavor, I am utilizing a Flex application to serve as a container for a web app. Within this Flex app resides a web controller that loads a complete Angularjs application. All necessary files are stored locally wi ...

Converting a JSON array to object properties with Mantle framework in iOS development

Currently facing a challenge in simplifying my Obj-C Model due to the complexity of the json data. I am struggling to convert array data into booleans. I have tried using the Framework's JSONKeyPathsByPropertyKey to define keys like @"gsRightIPServer ...

How can elements be collapsed into an array using the Reactive approach?

Consider this TypeScript/Angular 2 code snippet: query(): Rx.Observable<any> { return Observable.create((o) => { var refinedPosts = new Array<RefinedPost>(); const observable = this.server.get('http://localhost/ra ...

A method for utilizing history.goBack in linked tabs while preventing the user from reverting to the previously selected tab

In my application, I have a settings modal that contains tabs which act as links to different paths. When the user clicks on the background outside of the modal, it triggers the history.goBack function. However, I noticed that if the user has already click ...

Inquiry about how TypeScript handles object property references when passed into functions

As a newcomer to TypeScript, I am exploring the creation of a range slider with dual handles using D3.js. I have developed a simple class for managing the slider objects: export class VerticalRangeSlider{ private sliderContainer: d3.Selection<SVGG ...

Is there a way to incorporate both max-width and min-width in a matchMedia query effectively?

I'm currently utilizing the matchMedia API in my JavaScript code to identify viewports, with the goal of reducing DOM manipulations. Instead of using display: none extensively, I am opting for a v-if directive from Vue to determine when elements are ...

Using JavaScript to transform radio buttons into checkboxes

I have a grouping of radio buttons and a checkbox displayed on the page as shown below <html> <head> <title>Languages</title> <script type="text/javascript"> </script> </head> <body> <spa ...

When the same component is conditionally rendered, it does not activate the mounted() hook

I am new to Vue and eager to learn. I am attempting to conditionally render a component like so: <div> <Map v-if="bool" mapSrc="src1.jpg" :idList="idList1" :data="dataVariable1"></Map> <Map v-else mapSrc="src2.jpg" :idList="idList ...