Subtract the initial character from the result

What is the best way to eliminate the first letter from a string in this scenario?

d[],e[], [dsh,sj]

After analyzing the data, I realized that I need to remove the first letter before every comma (,). I tried storing the data and using a for loop, but encountered an error.

*Uncaught SyntaxError: Unexpected token [*

I'm having trouble understanding why this error is occurring. Can anyone explain?

EDIT : I understand how to remove elements, but I'm struggling with declaring it in this context.

Expected Output :  [],[],[dsh,sj]

Answer №1

To ensure we are on the same page:

let s = "hello"; // this is a String, note the use of quotes
let a = [] // an empty Array
let b = ["hello", "bye"] // an Array with two elements

If the provided input is:

let input = "d[],e[], [dsh,sj]";
let output = input.replace(/.\[/g,"["); // resulting in '[],[],[dsh,sj]' but as a String data type

// this code block might be improved...
let splitted = output.replace(/],/g, "],,");
splitted = splitted.split(",,");
let array = [];

splitted.forEach(function(value) {
  value = value.replace(/\[|\]/g,"");

  if (value === "") {
    value = [];
  } else {
    value = value.split(",");
  }

  array.push(value);
});

console.log(array); // [[],[],["dsh","sj"]]

I suggest checking out: http://www.w3schools.com/js/default.asp

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 function call to 'import firebase.firestore()' results in a value

I recently set up a Vue App with the Vuefire plugin. Here is an example of my main.js file, following the documentation provided at: : import Vue from 'vue' import App from './App.vue' import router from './router' import sto ...

Retrieve information from a JSON file containing multiple JSON objects for viewing purposes

Looking for a solution to access and display specific elements from a JSON object containing multiple JSON objects. The elements needed are: 1) CampaignName 2) Start date 3) End date An attempt has been made with code that resulted in an error displayed ...

Using arrays as data in an Ajax request for DataTables

Struggling to send selected row IDs to an API using the DataTables Ajax option. Despite numerous attempts, I can't seem to get the IDs sent as an array. Below is a sample code I've tried. It generates the URL: getStatistics.php?ids=1&ids=2& ...

Error: Router service provider not found in Angular 2 RC5!

Having trouble with using this.router.navigate. Here is the content of my app.module.ts file: import {NgModule, NgModuleMetadataType} from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; im ...

Testing Vue Components - Simulating the return value of a plugin

I have a unique scenario where I need to mock the return value of a custom plugin without importing it directly into my test. By creating a mock function for the plugin, I can easily achieve this goal. However, I am unsure how to change the return value of ...

Unable to display MongoDB collection in React application

I've been working on showcasing my projects using React and Meteor. I have two collections, Resolutions and Projects. The issue I'm facing is that while I can successfully display the Resolution collection on the frontend, I'm struggling to ...

Converting a stringified array of objects into an actual array of objects using Javascript

After receiving a HTTP response, I am faced with the challenge of working with the following variable: let data = '[{name: "John"}, {name: "Alice"}, {name: "Lily"}]' Although there are more objects with additional properties, this snippet provi ...

Rendering in ThreeJS Causes IE11 to Crash

I encountered a peculiar issue with Internet Explorer 11 while working on WebGL programming. Everything was functioning smoothly in all browsers until, out of the blue, IE started crashing when altering the positions of 4 meshes, without pointing to any sp ...

Trouble with implementing an onclick event listener in a foreach loop

While generating and adding HTML in a for loop, I noticed that adding onclick events within the same loop is not functioning correctly: items.forEach(item => { itemHtml = `<div class="${item.id}">${item.id}</div>`; $(".it ...

Angular2: the setTimeout function is executed just a single time

Currently, I am working on implementing a feature in Angular2 that relies on the use of setTimeout. This is a snippet of my code: public ngAfterViewInit(): void { this.authenticate_loop(); } private authenticate_loop() { setTimeout (() =& ...

Navigating through nested JSON objects in React to display data effectively

I have been struggling for hours to find a solution to this problem with no success. I really need your assistance. The task at hand involves looping through a JSON file and creating a user interface that consists of multiple columns, each containing vari ...

Tips for submitting an Ajax Form with identical Name attributes?

One part of my form consists of input fields with the same 'Name' values that will be stored as an Array. I am attempting to send these values via AJAX to PHP for updating my database. The challenge I'm facing is figuring out how to send t ...

PdfMake's loading speed is significantly impacted by the use of customized fonts in large sizes

After customizing the font in pdfMake, I encountered an issue. I successfully customized the font and generated a new vfs_fonts.js file containing Roboto and simfang (Chinese font). However, the file size is about 15MB, causing the system to load very sl ...

JavaScript Code for Executing Function on Checkbox Checked/Unchecked

My goal is to display an image when the checkbox is checked and show text when it is unchecked. However, I am facing an issue where the text does not appear when I uncheck the checkbox. <input type="checkbox" id="checkword" onchang ...

Executing an external Python script within a Vue application's terminal locally

Hello, I am new to using Vue.js and Firebase. Currently, I am working on creating a user interface for a network intrusion detection system with Vue.js. I have developed a Python script that allows me to send the terminal output to Firebase. Right now, I a ...

There are certain lines of JavaScript/Node.js code that are failing to execute

app.get is not being executed. I have also attempted to include app.listen(3000). My goal is to retrieve the parameter passed from the first web page. This code is designed to fetch parameters sent by another web page and then construct a MySQL query and ...

Unlimited loading on middleware when implementing it within a post route

I have successfully implemented a middleware that checks if a user has submitted their data to a form already. If they have, it redirects them to an error page with a link to access their previous results if desired. Below is the middleware I created: modu ...

Using Three.js and WebGL to create transparent layers that conceal the planes positioned behind them

Have you ever noticed that in Three.js / WebGL, when you have two planes and one or both are transparent, the plane behind can be hidden by the transparent plane on top? Why does this happen? ...

Populate a PHP array by cross-referencing it with data from a MySQL

In this MySQL output, we are displaying the count of a particular disease for each month. I have a PHP array with 12 months and I need to compare it with the table data. If there is no data for a specific month, I want to add a '0' value in a new ...

Is it possible to prevent a line break in a div tag, or are there other options available?

On my ASP.NET 4 / VB website, I encountered a scenario where I needed to incorporate a class called "noprint" in my footer, as specified in the print.css file. However, there was already a span class present, so I ended up enclosing div tags around it. Mor ...