Checking for a condition on an array using loops in JavaScript

I have a question about setting boolean conditions for data retrieved from an array. Here is the code snippet:

for (var k in GexfJS.graph.edgeList) { 
    var _edge = GexfJS.graph.edgeList[k]
    if ( (_edge.source == _curre) && ( _edge.target != _nodeIndex) ) {
       var _node = GexfJS.graph.nodeList[_edge.target]; 
       //action
    }
}

In the algorithm above, I want to set a condition for each _edge.target. If a certain action is taken, it should change a variable check to true (boolean condition).

For example:

var check = false;
for (var k in GexfJS.graph.edgeList) { 
    var _edge = GexfJS.graph.edgeList[k]
    if ( (_edge.source == _curre) && ( _edge.target != _nodeIndex) && (check != true) ) {
        var _node = GexfJS.graph.nodeList[_edge.target];
        //action
        check = true;
    }
}

The issue is that the check condition does not consistently apply to the _edge.target.

Answer №1

Avoiding the use of Boolean conditions and instead utilizing an object,

if ( (_edg.target == _curra) && (_edg.source != _nodeIndex) && (_edg.target != _n)) {
                                    var _nod = GexfJS.graph.nodeList[_edg.source];
                                    if(check[_nod.label] != true){
                                        if(_n != _nod){
                                        _str += '<li><div class="smallpill" style="background: ' + _nod.color.base +'"></div><a href="#" onmouseover="GexfJS.params.activeNode = ' + _edg.target + '" onclick="displayNode(' + _edg.target + ', true); return false;">' + _nod.label + 'b</a>' + ( GexfJS.params.showEdgeWeight && _edg.weight ? ' [' + _edg.weight + ']' : '') + '</li>';
                                        }
                                    }
                                    check[_nod.label] = true; 
                                }

Remember to declare var check={};

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

The Material UI read-only rating component fails to update even after the data has been successfully loaded

I have a rating component in my child component, and I am passing data from the parent component through props. However, there seems to be an issue with the MUI rating value not updating when the data is ready for viewing. https://i.sstatic.net/bqSfh.jpg ...

Utilize PHP variables within an array using colons

Working with variables in this array structure is proving to be quite a challenge for me. While I am well-versed in using associative arrays and incorporating variable values in them, the colon structure presented here is new territory for me. I am lookin ...

invoke two JavaScript functions without displaying any message

I'm facing an issue with Ajax as it's not displaying the message I intend to show. Let me elaborate. Within my PHP code, there is an input tag: <input type="submit" id="login_button_add" name="submit" value="Add" onclick="add_building(); sho ...

The folder cannot be created due to an invalid argument provided for the mkdir function

Having trouble running this code to create a folder that doesn't exist, while testing code updates from v13 to v14 and implementing slash commands. I'm stuck at this point. var dir = `./cha/${"<@" + interaction.member.id + "> ...

Tips for customizing the ion-alert header in Ionic framework

I have recently created an alert controller and am struggling to customize the appearance of the header within the alert popup. Despite going through the documentation, I have not been able to find a suitable solution. async customizeAlert(header, subHea ...

What could be causing the responsive line chart from nivo to not appear in jsdom while using Jest?

I am currently working on writing UI tests for my application using Jest/Testing Library. In the testing process, I have integrated the ResponsiveLine component from the @nivo/line library. However, I am facing an issue where the Responsive Line componen ...

When utilizing the getIntersectionList function, IE 9 may experience a malfunction and cease functioning

I am currently working with SVG code and JavaScript, trying to achieve a specific intersection result. <svg id="svgSurface" width="500" height="500"> <defs> <marker id="Triangle" viewBox="0 0 20 20" refX="0" refY="0" markerUnits ...

Guide on choosing a date from a datepicker that is always 21 days ahead of the current date using Selenium and Java

Here is the code snippet I am working with: SimpleDateFormat df = new SimpleDateFormat("dd/MM/YYYY"); Date dt = new Date(); Calendar cl = Calendar.getInstance(); cl.setTime(dt); cl.add(Calendar.DAY_OF_YEAR, 21); dt=cl.getTime(); ...

Tips for fixing the issue of "module ./response not found" in Node.js Express

Whenever I execute the command $ npm start this error message appears > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8feefcfce6e8e1e2eae1fbbccfbea1bfa1bf">[email protected]</a> start > nodemon server.js ...

Personalized configurations from the environment in the config.json file

I need to dynamically populate a setting object in my config.json file based on environment variables. The settings should vary depending on the environment. "somesetting": { "setting1": "%S1%", "setting2": "%S2%" } I am currently working on Wind ...

Unlocking the value of the "input" field within an EventListener function in Angular directly from the DOM

In my "characters" module, there is a form with a text field and a button. When the button is clicked, it triggers a function but I am struggling to retrieve the current input text and pass it to the async function. HTML: https://i.sstatic.net/DMF8w.png ...

Looking to save a series of string values into an integer variable instead of a char array that is limited to 4 bytes in

I am currently storing IP address values in a char array (uint8_t) with a size of 4 bytes. However, I would like to store these values in a single integer. Can anyone provide guidance on how to achieve this? uint8_t ip[4]; str = "192.168.1.1"; ...

Navigating does not involve contacting components

import React from "react"; import Navbar from "./components/Navbar/navbar.component"; import HomePage from "./pages/homePage/HomePage.component"; import Footer from "./components/Footer/footer.component"; import BlogPage from "./pages/blogPage/BlogPage.com ...

Troubleshooting the inability to perform deep assignment on a Javascript object in NodeJS

My Objective I am attempting to extensively re-map an existing array of objects using a mapping function. Although the mapping itself is functioning correctly, the deep assignment of the object is not taking place (refer to "output 2"). (Background) Backg ...

Creating a JavaScript automated slideshow with multiple instances of loops and/or setInterval running simultaneously?

Currently, I am facing a challenge while trying to create a basic automated slideshow from scratch. Though I have successfully built manual slideshows in the past, automating them is proving to be more complex. I started by experimenting with a for loop st ...

Eliminate the final comma from a two-dimensional array by utilizing a for loop

Is there a way to prevent the final comma from being printed? public void displayArrayMethod(int array[][]) { for(int row=0; row<array.length; row++) { for(int column=0; column<array[row].length; colu ...

Finding differences between two 24-hour format times using moment.js

Is there a way to compare two times in 24-hour format using the code below? $("#dd_start_timing, #dd_end_timing").on('keyup change keydown', function() { var DutyDayStartTime = $("#dd_start_timing").val().trim();// 13:05 var ...

For each variable, assign a value

Apologies if this question has been asked before, but I'm encountering an issue with a foreach loop in JavaScript. let fields = temp_deviceAllocation.devices.forEach(async (device) => { const fields = await device_model .findOne({ dev ...

What is the best way to create a linear swept face in three.js?

Is there a way to create a linear swept face with code? I have written the following code: // defining the profile var points = [ new THREE.Vector2(0, 0), new THREE.Vector2(10, 0), new THREE.Vector2(10, 10), ...

Utilize AJAX request to populate a datatable with JSON data and incorporate hyperlinks for seamless

Below is the datatable java script I am using: $('#Proj_emp_table').dataTable({ "sAjaxSource": "/projects/project_json/", "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) { oSettings.jqXHR = $.ajax( { ...