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

Angular2 bootstrapping of multiple components

My query pertains to the following issue raised on Stack Overflow: Error when bootstrapping multiple angular2 modules In my index.html, I have included the code snippet below: <app-header>Loading header...</app-header> <app-root>L ...

Can VueJS Computed handle multiple filters at once?

I am encountering an issue with this code - when I attempt to add another filter inside the computed section, it doesn't work. However, if I remove the additional filter, the code functions correctly. My goal is to have both company and product searc ...

Tips for managing intricate JSON data in Python, especially when the keys are not guaranteed to be present

I am looking for assistance in extracting the value of Key issues from a JSON object, which may not always be present. The JSON structure I have is: { "records":[ { "previousAttempts": [], "id": "aaaaa-aaaa ...

Shape with a dark border div

Having some trouble with creating this particular shape using CSS border classes. If anyone can assist in making this black box shape using Jquery, it would be greatly appreciated. Check out the image here. ...

Modify the tooltip of the selected item in an ng-repeat loop in AngularJS

Upon clicking an element, a function is executed which, upon successful completion, should change the tooltip of that specific element. Within an ngRepeat loop, I have multiple elements displaying the same tooltip. However, I only want to update the toolt ...

Attempting to modify read-only properties is prohibited in strict mode within the context of [background: url({{XXX}}) no-repeat center center

I encountered an issue in Edge, but everything works fine in Chrome. I can't figure out what's causing the problem... <div class="container-fluid project_img" style="background: url({{_project.images.web}}) no-repeat center center;"> ...

Ajax request triggers a page refresh

As I review the AJAX call within a React method, there are some key observations: handleActivitySubmit: function handleActivitySubmit(activity, urlActivity, callbackMy) { console.log("querying with activity: " + JSON.stringify(activity)); $.ajax ...

Is it better to use Asynchronous or Synchronous request with XMLHttpRequest in the Firefox Extension for handling multiple requests

As a newcomer to developing Firefox Add-Ons, my initial project involves calling an external API in two steps: Step 1) Retrieve data from the API. Step 2) Use the data retrieved in Step 1 to make another call to the same API for additional information. ...

Multer is experiencing difficulties in uploading files, yet it is not displaying any error messages

When setting up an application to create courses with images, I encountered an issue while using multer for image uploading. Despite adding multer to my route with upload.single('images'), the uploaded images were not appearing in the designated ...

What is the best way to create a gradual color change in each individual cell instead of changing all at once?

Looking for some help with a grid I'm working on. I've got the cells changing colors like I want them to, but they're all changing at once. What I really need is for them to light up in a specific order - Yellow, Green, Blue, White, Orange. ...

Pressing an ASP.NET submit button triggers a jQuery Ajax call for closing purposes

I am currently developing a webpage in asp.net that utilizes JQuery UI dialog. This dialog contains a submit button which, when clicked, closes the dialog. I am looking to incorporate a call to a Webmethod upon submission. If the method returns true, then ...

GAS: What strategies can I implement to optimize the speed of this script?

I have a sheet with multiple rows connected by "";" and I want to expand the strings while preserving the table IDs. ID Column X: Joined Rows 01 a;bcdfh;345;xyw... 02 aqwx;tyuio;345;xyw... 03 wxcv;gth;2364;x89... function expand_j ...

Returning Props in Dynamic Components with Vue 3

Exploring the capabilities of Vue3's Dynamic Component <component>, I am currently working with this setup: Component 1: <template> <div> <h1> Name Input: </h2> <Input :model="props.name" /> ...

What is the best way to specify option values for selection in AngularJS?

...while still maintaining the model bindings? I currently have a select menu set up like this: <select class="form-control" ng-model="activeTask" ng-options="task.title for task in tasks"> </select> When an option is selected, it displays s ...

Issue with reactivity not functioning as expected within VueJS loop without clear explanation

Struggling with implementing reactivity in vue.js within a loop? The loop renders fine, but firing an event updates the content without visibly rendering the data on the page. The latest version of vue.js is being used with bootstrap and jquery. Despite a ...

Having trouble with the error "Cannot GET /" in your Angular2 and Express

I've been working on customizing this GitHub example application to utilize Express instead of KOA. When I enter gulp serve in the CentOS 7 terminal, the app launches successfully. However, upon typing http : // localhost : 8080 in the browser, a 404 ...

Endless Loop Issue with Google Maps API Integration in NextJS-React

Currently struggling to troubleshoot an infinite loop issue in my React function component map. I've spent a considerable amount of time analyzing the code and suspect that it might be related to the useEffects, but I'm unable to resolve it. Atta ...

Using JavaScript to save a file with a data URL in Internet Explorer

Need help with file conversion on different browsers. I developed an app that converts files, and everything was working perfectly in Chrome. However, when it comes to IE (10/11/Edge), I'm facing some challenges: 1) The HTML5 download attribute does ...

Creating an array by extracting items from a textarea that are separated by line breaks

I am working with a textarea that has the id #list : $text = $('#list').val(); The goal is to split this text into pieces and place each piece as a new item in an array. This array will then be sent to PHP, where I can utilize it in a foreach l ...

How can we programmatically trigger a click action on an element obtained through an HTTP request with the help of Set

window.addEventListener("load" , () => { setInterval(() => { let xhttp = new XMLHttpRequest(); xhttp.open("GET", "./messagedUsers.php", true); xhttp.onload = () => { if(xhttp.re ...