Unexpected outcomes in linq.js

Hey there, I have a JSON object named "Faults" that looks like this:

"Faults":[{"RoomId":1,"ElementId":173,"FaultTypeId":1,"Count":1,"Remark":""},{"RoomId":3,"ElementId":211,"FaultTypeId":7,"Count":1,"Remark":""},{"RoomId":4,"ElementId":173,"FaultTypeId":1,"Count":1,"Remark":""}]

When I check the Faults object in the debugger, it shows up like this:

Now, my task is to determine if a room contains a Fault by using its RoomId. The code I'm currently using for this purpose is as follows:

    Enumerable.From(audit.Rooms).ForEach(function(room, index) {//√
    var containsFaults = '';

    //room.Id always has a value and cannot be null
    var test1 = faults.Select("$.RoomId==" + room.Id).Count();
    var test2 = faults.Select("$.RoomId==" + room.Id);

    if (faults.Select("$.RoomId==" + room.Id).Count() > 0) {
        containsFaults = '√';
    }

However, when I run this code, the results are not as expected...

I'm puzzled why it's not returning the faults from my object with matching RoomId. The Id's definitely match. Can anyone point out what mistake I might be making here? I'm really stuck on this issue...

Appreciate any help you can provide!

Answer №1

To respond to the inquiry

Why is it not displaying the faults from my object with the corresponding RoomId? I'm certain the Id's match. What am I missing here, I'm really struggling with this...

You must include .ToArray() to showcase the result.

var test2 = faults.Select("$.RoomId==" + room.Id).ToArray();
//                                               ^^^^^^^^^^

var audit = { Faults: [{ RoomId: 42, ElementId: 4711, FaultTypeId: 0, Count: 0, Remark: "no fault" }, { RoomId: 1, ElementId: 173, FaultTypeId: 1, Count: 1, Remark: "" }, { RoomId: 3, ElementId: 211, FaultTypeId: 7, Count: 1, Remark: "" }, { RoomId: 4, ElementId: 173, FaultTypeId: 1, Count: 1, Remark: "" }] },
    roomId = 4,
    dataset = Enumerable.From(audit.Faults),
    test1 = dataset.Where("$.Count > 0 && $.RoomId==" + roomId).Count();
    test2 = dataset.Where("$.Count > 0 && $.RoomId==" + roomId).ToArray();

console.log(test1);
console.log(test2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

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

Is it possible to analyze an API call and determine the frequency of a specific field?

Code: var textArray = new Array(); var allText = results.data._contained.text; for (var i = 0; i < allText.length; i++) { var text1 = allText[i]; var textHtml = "<div id='text_item'>"; textHtml += "& ...

Is there a way to switch the language on the date-picker tool?

My date-picker has a unique set up: <input type="text" ng-readonly="true" class="form-control" datepicker-popup="yyyy-MM-dd" ng-model="filter.FInicial" is-open="filter.controlFInicialAbierto" min-date="minDateIni" max-date="maxDat ...

What is the best way to halt a specific function in jQuery?

I recently developed a website for my photographer friend, and I implemented a feature where the home page is initially blurred. Visitors can click a button to unblur the content. While the functionality works smoothly, there's one issue - every time ...

React's connect method is causing issues with my test case

Attempting to create a test case for my jsx file... Took a sample test case from another jsx file... The other file does not have the connect method... But this new file contains the connect method... Believe this is causing issues with my test case... Any ...

Setting a JavaScript variable to null

Using Jquery, I have a variable that is assigned a value on button click. After the code executes successfully, I need to reset the variable to null. $("#btnAdd").click(function () { var myDataVariable= {}; myDataVariable.dataOne="SomeDa ...

Error: unexpected syntax error found in variable `author`

Here is a snippet from my PHP file: if($operation=='upload'){ if(isset($data->paper) && !empty($data->paper) && isset($data->paper->author) && isset($data->paper->description) && isset($data->paper->title){ ...

Leveraging @click within dropdown selections - Vue.js 2

Is there a way to implement the @click event in select options? Currently, I have the following: <button @click="sortBy('name')">sort by name</button> <button @click="sortBy('price')">sort by price</button> Th ...

Upon triggering a GET request to the URL "http://localhost:3000/search", a "404 (Not Found)" error response was received. Interestingly

Can Someone assist me please? I'm having trouble creating a website similar to YouTube and encountering the following error: GET http://localhost:3000/search 404 (Not Found) Uncaught (in promise) Error: Request failed with status code 404 at createEr ...

Error message stating that there is no property 'collection' in Firestore when using Firebase v9 modular syntax in Firebase Firestore

Working on a React application that makes use of Firebase Firestore for handling database operations, I recently upgraded to Firebase version 9 and adopted the modular syntax for importing Firebase services. Nevertheless, when attempting to utilize the co ...

Rails issue: Active Model Serializer searching for incorrect serializer

Every time I try to use the create method, it throws a NameError at me. Failure/Error: post :create, { user: { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a7aca5b6a8a1b784a1bca5a9b4a8a1eaa7aba9">[email ...

Switch from using jQuery to vanilla JavaScript for handling POST requests

I am looking to eliminate the jQuery dependency and use plain JavaScript instead in the code below for a post request. <script type="text/javascript> $(function() { $.post("test_post.php", { name: "John Doe", ...

What is the best way to retrieve the value of a property within a JavaScript object?

I am facing an issue with retrieving the value of the status property from an object in my code. Below is a snippet of what I have tried: console.log("Resource.query()"); console.log(Resource.query()); console.log("Resource.query().status"); console.log(R ...

The animation unexpectedly resets to 0 just before it begins

Currently, I am developing a coverflow image slider with jQuery animate. However, there are two issues that I am facing. Firstly, when the animation runs for the first time, it starts at `0` instead of `-500`. Secondly, after reaching the end and looping b ...

Retrieve the name of the path for the specified * stack within Express.js

When working with Express.js, I am utilizing '*' to catch the error 404. Is there a way for me to find out the path name of the error URL? app.get('*', (req, res) => { console.log("route: " + JSON.stringify(req.route) ...

What is the best way to capture the input value upon pressing the "Enter" key?

My first question here is about implementing the addtodo(todo) code. After trying it out successfully, I wanted to make it work when typing and pressing enter. However, despite attempting some other methods, I couldn't get it to work. I didn't re ...

Is it possible to continuously update a Google line chart by programmatically adding rows at specific intervals using an AJAX call

Is there a way to dynamically add rows to the Google line chart each time data is retrieved via an AJAX call at set intervals? This is the existing code: google.charts.load('current', {'packages':['line']}); google.charts. ...

What exactly constitutes a circular data structure within JSON?

I'm encountering an issue in my Express app where I am receiving the following error: UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON Despite searching for similar problems, I am struggling to grasp the concept o ...

Employing the JSON return code

I am attempting to implement ajax with php. Here is the PHP script I have: <?php // This file retrieves the POST information sent by an AJAX request and returns the values if successful. $price['name'] = "Called"; $price['Wheel'] ...

Is it possible to use both "npm install --global" and "--save" simultaneously?

I'm curious if it is practical to use both the --global and --save parameters in the npm install command simultaneously. For instance: npm install gulp -g -s From my understanding, since there is no package.json in the npm system folder, I assume th ...

Sending parameters from one Node.js function to another in separate JavaScript files

I am currently working on passing function responses between two Node.js scripts. Here is my approach. Index.js var express = require('express'); require('./meter'); var app = express(); app.get('/',function(req,res){ ...