Combining intricate arrays in two dimensions

When I retrieve a row from a table on the UI, it shows only one row. Here are the values from the UI table displayed on my console:

[ '  0',
  'ALPHA NUMERIC(100)\nPG_8_2670\nSt.Athens.,Pt\n1062645\nAutomation-if add row is editable\nALPHA NUMERIC(20)\nTIMESTAMP(19)\nYYYY-MM-DD(19)\n0' ]

My objective now is to convert the above result into the format shown below.

[ '  0','ALPHA NUMERIC(100)','PG_8_2670','St.Athens.,Pt','1062645','Automation-if add row is editable','ALPHA NUMERIC(20)','TIMESTAMP(19)','YYYY-MM-DD(19)','0' ]

The issue I am encountering involves the comma in 'St.Athens.,Pt'. I've tried the code below, but it hasn't resolved the problem:

client.getText(obj.table.addedRow,function(err, values) {
                console.log('Values before changes:',values);
                var str = values.toString();
                str = str.replace(/\n/g,",");
                var values = str.split(',').toString();
                console.log('Values from table of empty row post changes:',values);

Answer №1

Seemingly, there is no necessity for these particular lines

  str = str.replace(/\n/g,",");
  values = str.split(',').toString();

An alternative approach that may prove to be more efficient is to split directly on the \n; like so

  values = str.split('\n');

However, it might be beneficial to handle the Array in a more organized manner; consider using a loop with pop-push or loop-push combination to achieve the desired result.

Below is a functional code sample:

var startArray = [ '  0',
  'ALPHA NUMERIC(100)\nPG_8_2670\nSt.Athens.,Pt\n1062645\nAutomation-if add row is editable\nALPHA NUMERIC(20)\nTIMESTAMP(19)\nYYYY-MM-DD(19)\n0' ];
var endArray = [];

startArray.forEach(function(key) { 
    if(key.indexOf("\n")>=0) {
        endArray = endArray.concat(key.split('\n'));
    } else {
        endArray.push(key);
    }
});

// voila, endArray contains 
// [ '  0','ALPHA NUMERIC(100)','PG_8_2670','St.Athens.,Pt','1062645','Automation-if add row is editable','ALPHA NUMERIC(20)','TIMESTAMP(19)','YYYY-MM-DD(19)','0' ]

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

Navigating through concatenated JSON strings in a web browser: A step-by-step guide

I am currently using Socket.IO to transmit data to the browser. The information being sent is a continuous stream of JSON objects, which upon arrival at the browser, transforms into a single large JSON string. However, the issue I am encountering is that t ...

What is the best way to create separate arrays for every element in my object, containing a total of four arrays to efficiently produce a weekly forecast?

Hey there, I'm back with an update on my ongoing project. I've encountered a major challenge while trying to incorporate a 7-day forecast display in my app. Something along these lines: https://i.stack.imgur.com/xJA4M.png https://i.stack.imgur. ...

Issues with implementing Bootstrap tabs in Vue application

Currently, I am in the process of developing an application using vue, vue-router, and bootstrap. My goal is to integrate tags in bootstrap as shown below: <ul class="nav nav-tabs" role="tablist"> <li class="nav-item"> <a class="nav-l ...

.slideDown Not Functioning Properly on my System

After successfully linking my index.html file to jQuery, I am facing an issue with the .slideDown code. I'm not sure if there is a problem with the code itself or if I didn't attach jQuery correctly. Can anyone help me troubleshoot this? Below i ...

Preserve the state of Bootstrap 5 tabs upon refreshing the page

<ul class="nav nav-pills" id="my_tabs"> <li class="nav-item" role="presentation"> <a class="nav-link active" data-bs-toggle="pill" href="#tab1" aria-selected=&quo ...

Is there a way to add to a dropdown option in select2?

I am in need of creating a dropdown feature that offers users the ability to: 1. Automatically fill in options (based on predetermined values) 2. Add a brand new option if none of the current choices are suitable. I came across select2 as an easy way to i ...

Tips for handling an InvalidSelectorException in Selenium when logging into a website

I've been working on automating website logins using Selenium and here is the code I have written: from selenium import webdriver driver = webdriver.Chrome() driver.get("https://abcde.com") assert "xxx" in driver.title user = driver.find_element_by ...

I have an array generated through a foreach loop and I am looking to now add it to

In my form, I have a foreach loop where I am fetching data using AJAX based on the NAME attribute. For example, the price is retrieved from an input field like this: $price = "price$fetch[id]"; <input type="text" id="<?php print $price;?>" name=" ...

Detecting collisions in JavaScript

Currently, I am in the process of reviving an old game reminiscent of Tron using HTML5 canvas and JavaScript. The unique twist in this version is that the snakes have the ability to move in curves rather than right angles (known as Achtung Die Kurve). The ...

Obtain latitude and longitude coordinates for the corners of a React Leaflet map

Currently, I am working with react-leaflet and facing a particular challenge: In my map application, I need to display pointers (latitude, longitude) from the database. However, retrieving all these pointers in one call could potentially cause issues due ...

Is it not possible to call a function directly from the controller in AngularJS?

I have been trying to update a clock time displayed in an h1 element. My approach was to update the time by calling a function using setInterval, but I faced difficulties in making the call. Eventually, I discovered that using the apply method provided a s ...

"Troubleshooting: Ajax File Uploader Plugin Not Functioning Properly

Today, our web site's file upload feature using the javascript plugin Simple-ajax-uploader suddenly stopped functioning (09/05/2019). The upload div/button is unresponsive when clicked. This issue is not limited to our site; even the official plugin ...

Is It Possible to Determine If a Checkbox Has Been Checked?

Here's what I have now: I have a checkbox that I need to verify if it's selected. <input type="checkbox" name="person_info_check" value="0" &nbps>Read and agree!</input> However, the method I found online to verify the checkbox ...

What are the steps for implementing function composition or pipelines with a multi-parameter function?

During a recent interview, I was tasked with retrieving data from the jsonplaceholder /posts and /comments endpoints and creating a function to match comments with posts where comment.postId == post.id, then constructing a unified JSON object containing ea ...

Give properties to a function that is being spread inside an object

While working with React, I am facing a challenge of passing props from the instanced component into the mockFn function. The code example below is extracted and incorporates Material UI, but I am struggling on how to structure it in order to have access t ...

Preserving color output while executing commands in NodeJS

My Grunt task involves shelling out via node to run "composer install". var done = this.async(); var exec = require('child_process').exec; var composer = exec( 'php bin/composer.phar install', function(error, stdout, stderr) { ...

Refining the options in security question dropdown menus

Firstly: The title should mention filtering security options in dropdown lists, but it seems I'm restricted from using the term questions or question in the title. I came across this code example, but it appears to be outdated. Does anyone know why a ...

JavaScript first, middle, and last names

When working with Javascript, I have encountered a challenge. I am attempting to extract the First, Middle, and Last names from a full name input into three separate fields - Character Length, Middle Name, and Initials. At this point, I have successfull ...

What is the best way to divide a PHP array into two separate arrays based on a certain condition

I need to divide a given array based on the sub_id This is my original array: $my_main_array = [ ['id' => 1, 'sub_id' => 8], ['id' => 2, 'sub_id' => 9], ['id' => 3, 'sub_id& ...

Is JSON.stringify() the standard object and function in JavaScript for converting objects to JSON?

This is the first time I've encountered this, but it appears to function smoothly even without the use of any JavaScript libraries or frameworks. Is this a built-in feature in JavaScript? If so, where can I locate documentation on this and other less ...