Does JavaScript array filtering and mapping result in a comma between each entry in the array?

The code snippet above showcases a function that retrieves data from a JSON array and appends it onto a webpage inside table elements.

//define a function to fetch process status and set icon URL
function setServerProcessesServer1761() {
  var url = "Server1761.json";
  var $svc = $("#Server1761-processes"); //get reference to <div>

  $.getJSON(url, function(data) {
    document.getElementById(
      "Server1761-processes"
    ).innerHTML = data.processes
      .filter(s => s.Name.value.includes("company_name"))
      .map(
        s =>
          `<table><tr><td>${s.Name.value}</td> <td>${
            s.Status
          }</td></tr></table>`
      );
    $("#Server1761-processes").html(function(_, html) {
      return html.replace(/runn1ng/g, "<img src='smallgreen.png' />");
    });
    $("#Server1761-processes").html(function(_, html) {
      return html.replace(/fai1ed/g, "<img src='smallred.png' />");
    });
  }); // you will have to concatenate the entire services request line on line 130
}

//jQuery syntax to run when document is ready 
$(function() {
  setServerProcessesServer1761();
});

Upon implementation, an issue arises where a comma , is added after each table row, as illustrated here:

https://i.sstatic.net/1g1ny.png

To resolve this, I initially checked for commas within the script but found none using CTRL+F. It then occurred to me that the commas were originating from the JSON entries in the array.

Hence, my query is how can I eliminate these unwanted commas from the JSON array so that the table rows appear seamless without breaks or commas like this:

https://i.sstatic.net/fArta.png

Answer №1

When you assign an array to the innerHTML property of an element

element.innerHTML = [4, 5, 6]

The array is automatically converted to a string and then displayed on the webpage.

By default, converting an array to a string will separate each element with a comma.

console.log(
  [4, 5, 6].toString() === [4, 5, 6].join(',')
)

You have the option to customize this behavior by specifying a different separator, such as a new line:

console.log(
  [4, 5, 6].join('\n')
)

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

Integrate cart feature similar to the shopping experience on Zopnow app

I'm working on integrating an add to cart feature similar to the one in the Zopnow app. Does anyone understand the concept behind this functionality? It's really impressive. ...

What is the best way to integrate Next.js with Strapi (or the other way around)?

My goal is to develop an application utilizing Next.js for the frontend, fetching data from a Strapi API hosted on the same server. The plan is to have Strapi handle API and admin routes, while Next.js manages all other routes. I intend to use fetch in Nex ...

Passing values from Vue3 child component to parent component

Hey there, I'm currently diving into the world of Vue and I'm working on a table that dynamically displays abbreviations. I've been trying to send a searchTerm from my child component to the parent component, but I'm struggling with ge ...

Is there a way to avoid waiting for both observables to arrive and utilize the data from just one observable within the switchmap function?

The code snippet provided below aims to immediately render the student list without waiting for the second observable. However, once the second observable is received, it should verify that the student is not enrolled in all courses before enabling the but ...

Unable to assign a value to a variable within a Mongoose callback function

I am struggling to comprehend why the variable "productsAvailable" still shows true even after being set to false. router.post('/api/transactions', (req, res) => { var productsAvailable = true for(var i=0; i<3; i++) { Prod ...

The Unicode range [uXXXX] in Regex is not recognized during the AJAX request

I am currently facing an issue with removing control characters (e.g. \u0005) from the ajax response string. I have implemented a regular expression for this purpose: msg = msg.replace(/[\x00-\x1F\x7F-\x9F]/g, ''); Inter ...

Save a CSV file into a list, locate a specific key within the list, and retrieve the fourth value associated with that key

After thorough revision, the question now reads as follows: Take a look at this content in database.csv: barcode,ScQty, Name , Qty ,Code 123456 , 3 ,Rothmans Blue , 40 ,RB44 234567 , 2 ,Rothmans Red , 40 ,RB30 345678 , 2 ,Rothmans Gre ...

Achieving a dynamic "Picture Presentation" feature with JavaScript/jQuery

Currently, I am in the process of developing a website that will serve as a presentation. My plan is to create a slideshow effect using JavaScript. While I have implemented some functions, I must admit that it is not very organized at the moment. The main ...

Can the Flash Configurator be converted to PHP or Javascript?

Considering converting this flash application into either PHP or JavaScript. Take a look at the example: In PHP, the page reloads every time the customer selects a color. With AJAX, it's challenging to pass the selected value to PHP. Are there any ...

Ways to implement two functions within a single onclick event

Is it possible to call two functions with onclick event? <input id = "test" onclick="func1()"> Can I add another function as well? How would I go about doing that? ...

Trouble with jQuery script causing initial word count issue in textarea upon page load

I have implemented a word count textarea jQuery script and I am trying to figure out how to update the word count onload to 2 when the text area initially displays "this example". Currently, it shows 0 words. Although I can set focus on it and move the c ...

Utilizing several carets in a single or multiple text areas and input boxes

Just a quick question... Can a textbox have two carets simultaneously, or can we have two separate textboxes both focused at the same time? I am aware of simulating this using keydown listeners but I'm specifically looking for visible carets in both ...

Unable to retrieve or remove cookie sent from Express on client side

My Express server is sending a cookie to the client that is not httpOnly. Despite this, the client is unable to access the cookie through document.cookie or see it in the Application tab on chrome dev tools. Interestingly, I am able to view the cookie in C ...

Steps for importing a text file into a two-dimensional array in Java

Hey there! I'm trying to figure out how to read a text file into a 2D array. I want to store each line in an array so I can later use it for calculating the sum of length and width. This is for a project related to the game of life. MY CODE public P ...

Looping through Checkbox controls in VBA

I need to handle 20 different checkbox options, each named Checkbox 30 through Checkbox 50. Every selection made on these checkboxes must be converted into 4 separate arrays. The arrays I am working with are structured as follows: Graph_1(2,20) Graph_2(2, ...

What is preventing jQuery UI from detecting jQuery?

I have successfully created a JavaScript widget that is capable of being embedded on any third-party website, in any environment. The widget relies on jQuery and jQuery UI for its functionality. After following the guidelines outlined in How to embed Javas ...

Setting a dynamic routerLink in Angular 2 based on a component's property-value

Recently, I developed a component with a <a> element and a routerLink property that I intended to set from the template in which the component is used. However, when attempting to do so, I encountered an error message stating 'Cannot read proper ...

Embed a function within a string literal and pass it to another component

Is there a way to pass a function defined in actions to an element? Reducer case 'UPDATE_HEADER': return Object.assign({}, state, { headerChildren: state.headerChildren.concat([action.child]) }); Action.js export const deleteH ...

Angular model is not receiving the value of a false checkbox

One issue I am facing is with a form that includes a checkbox bound to an ng-model property. The problem arises when the checkbox is checked, as the value gets set in the model. However, when it is unchecked, the key and value remain unset, causing API val ...

What is the best way to organize a flatlist for rendering?

I'm struggling with separating some flat-lists into different components. How can I arrange the rendering of the flat-list like the sample form (Picture "Sample UI")? I've tried, but it's not working correctly as it renders flat list A first ...