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:

https://i.sstatic.net/30GJA.png

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...

https://i.sstatic.net/91zFH.png

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

Ways to retrieve an array field from a query result in MongoDB

Upon receiving a JSON object in Cursor, the structure is as follows: { "field1": "val1", "field2": [ { "field3": "val3" }, { "field4": "val4" } ] } A ...

What is the best way to organize an HTTP route using filters in a typical manner?

I developed a web service that offers various HTTP routes, one of which is formatted as follows: /grid/get-foos?filters={"type": ["bar"], "status": ["baz", "qux"]} The filters parameter consists of a serialized JSON object. It contains a defined set of k ...

When my route in NextJS processes the request, it returns a ReadableStream from req

I am encountering an issue with my code and I have tried looking for solutions in other similar questions without any success. Is there anyone who can assist me? Here is the content of my route.js file: import dbConnect from "@/lib/dbConnect"; i ...

Utilize Javascript to establish a fresh attribute by analyzing other attributes contained in an array within the object

Currently, I am working on a data structure that looks like this: masterObject: { Steps: [ { step: { required: false, }, step: { required: false, }, step: { required: false, }, }, ] } ...

Converting time from 00:00:01 to a format of 8 minutes and 49 seconds in Angular

Is there a way to transform a time value from 00:00:01 (not a date object) into a format showing 8 minutes and 49 seconds? Even after consulting the Angular 'date pipe' documentation, I couldn't find a solution to this issue. ...

Looking for tips on how to achieve a sleek search bar expansion effect when focusing using w3.css?

I have been trying to utilize the following code to create a Google search bar that expands when focused within a w3.css navigation bar-item. While it does expand and contract on focus/blur, I am unable to achieve smooth transitions between sizes. The sear ...

How to extract data from URLs in Angular

Looking into how to extract a specific value from the URL within Angular for parsing purposes. For example: http://localhost:1337/doc-home/#/tips/5?paginatePage=1 The goal is to retrieve the value "5". HTML snippet: <a href="#/tips/comments/{{ tip ...

How do RxJS collection keys compare?

Is there a more efficient way to compare two arrays in RxJS? Let's say we have two separate arrays of objects. For example: A1: [{ name: 'Sue', age: 25 }, { name: 'Joe', age: 30 }, { name: 'Frank', age: 25 }, { name: & ...

Executing a Vue method from the main.js file within a Vue.js document

Why can't I call my method in App.vue? Shouldn't the div with id='App' in the App file allow me to access methods within it? Main.js new Vue({ render: h => h(App), methods:{ gesamt:function () { return 'Hello&a ...

Ways to retrieve the neighboring element's value and modify it with the help of JavaScript or jQuery

I am encountering a problem with selecting an adjacent element and updating its value. My goal is to update the input value by clicking the minus or plus buttons. I have successfully retrieved all the buttons and iterated through them, adding onclick eve ...

`Count the number of rows needed to accommodate text line breaks within a div element`

Is there a method to determine the number of lines that text breaks into using the CSS property word-break: break-all? For example, if I have a div like this: <div>Sample text to check how many lines the text is broken into</div> And the corr ...

Activate the mouseenter event as soon as an animated element makes contact with the cursor

As I venture into the world of web development, I am currently working on creating a game where the main objective is to avoid touching moving circles with the cursor. My approach involved using a mouseenter event as shown in the JSFiddle example below: $ ...

Dealing with substantial ajax responses using JavaScript

Currently, I am in the process of developing a website that utilizes jQuery File Tree. However, there is an issue with the enormous size of the AJAX response from the server - 900 KB and containing approximately 70,000 'files' (which are not actu ...

Is there a different way to invoke PHP within JavaScript?

Is it possible to include PHP code in JavaScript? I have come across information stating that this practice is not recommended: document.getElementById('id1').innerHTML="<?php include my.php?>"; If including PHP directly in JavaScript is ...

Accessing a JSONObject deep within two layers of JSONArrays

I am in the process of developing a calendar system. Within my code, I have created a base "year" JSONArray that consists of twelve "month" JSONArrays, each containing the corresponding number of "day" JSONObjects. Below is the code snippet I used to achie ...

Transform your HTML audio player into a Vue component

I am in the process of converting an HTML player into a Vue component. Half of the component has been successfully converted, but the time control slider is still missing. Below is the original HTML code for the player: // JavaScript code for the audi ...

Using npm properties reader to access a key with multiple suite values from a properties file in Protractor

Recently, I had to find a way to store some data outside of the standard conf.js file. To tackle this requirement, I decided to use the properties-reader node module. However, I encountered some issues when trying to access certain key values. Below are e ...

Enhance functionality in JavaScript by accessing the return value from a function

I'm trying to achieve the same outcome using a different method. The first approach is functioning correctly, but the second one is not: <script> function hello(){ var number = document.getElementById("omg").innerHTML; if(numbe ...

Having trouble decoding XML files with missing content in Azure Logic Apps

I have encountered an XML file with the following structure: <?xml version="1.0" encoding="UTF-8"?> <Data> <datym> <bla bla> </datym> <datym> <bla bla> </datym> </Data> While I c ...

Is OnPush Change Detection failing to detect state changes?

Curious about the issue with the OnPush change detection strategy not functioning properly in this demonstration. My understanding is that OnPush change detection should activate when a property reference changes. To ensure this, a new array must be set e ...