A method for separating arrays within a string based solely on the commas between each one

I am working with a string containing an array of arrays that needs to be split into individual arrays.

array = "[['<1>', 'likes'], ['<2>', 'reads'], ['<3>', \"doesn't have\"]]"

This is what I have attempted so far:

array.split(",")

The desired output should be:

[['<1>','likes'], ['<2>', 'reads'],['<3>', \"doesn't have\"]] 

Answer №1

Looking to divide this string into multiple arrays

Following Nick Parsons' advice, adjust the quotes to make them valid JSON and utilize JSON.parse()

const array = `[["<1>", "likes"], ["<2>", "reads"], ["<3>", "doesn't have"]]`
console.log(JSON.parse(array))

If you find it challenging to reformat the input string, you can substitute single quotes with double quotes using this intricate regex pattern that required more time than expected to create:

const array = "[['<1>', 'likes'], ['<2>', 'reads'], ['<3>', \"doesn't have\"]]"
const arrayJSON = array.replace(/(?<=[\[\]\, ])'|'(?=[\[\]\, ])/g, `"`)
console.log(arrayJSON)
console.log(JSON.parse(arrayJSON))

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

How can I access a nested FormArray in Angular?

I have a situation where I am trying to access the second FormArray inside another FormArray. Here is an excerpt from my component: registrationForm = new FormGroup({ registrations: new FormArray([this.patchRegistrationValues()]) }); patchRegistrati ...

Updating parent data in Vue.js does not automatically trigger an update in the child component

I have the following: Vue.component('times-updated', { template: '<span>Times Updated: {{ timesUpdated }}</span>', data: function() { return { timesUpdated: this.$parent.myData.timesUpdated ...

Verify whether the element appears twice in the array

Is there a way to determine if an element appears more than once in an array? var arr = [elm1, elm2, elm3, elm3, elm4, elm5, elm5, elm5, elm6, elm7]; if (elm appears multiple times in the array) { // code to be executed } else { // do somethin ...

How to utilize jQuery to replace the first occurrence of a specific

Suppose I have an array structured like this: var acronyms = {<br> 'NAS': 'Nunc ac sagittis',<br> 'MTCP': 'Morbi tempor congue porta'<br> }; My goal is to locate the first occurrence ...

Bootstrap.js has the ability to utilize nested objects for organizing and

As I work on enhancing a Combobox class I developed to complement Bootstrap 4, I am aiming to align the Javascript with the existing Bootstrap code. During this process, I came across a snippet of code in bootstrap.js while studying the Modal component: ...

Program in ANSI C that utilizes an array of characters in both a socket client and socket server

As I delve into creating a basic client and server using sockets in C, the process involves the client transmitting the size of the character array (including the last cell for '\0') and subsequently sending the array of characters. On the s ...

Encase the event handler within JQuery

Here's an example of inputs with OnBlur event handlers: <input name="abc" tabIndex="5" class="datetime" onblur="if (CheckMode(this))__doPostBack('abc',''); else return false;" /> Now, in JQuery Form ready function, I want ...

When I start scrolling down, the Jumptron background image vanishes

Utilizing bootstrap jumptron with a background image has led to an issue where the background image disappears as I scroll down, leaving only the jumptron div class displaying the heading. How can this be fixed so that the image remains visible even when s ...

Showing the number of times a button has been pressed

I have written some HTML code to create a button and now I am looking for guidance on how I can use Vue.js to track how many times the button has been clicked. Here is what I have so far: <div class="123"> <button id = "Abutton&q ...

What is the best way to title an uploaded chunk with HTML5?

Here is the script I am working with: function upload_by_chunks() { var chunk_size = 1048576; // 1MB function slice(start, end) { if (file.slice) { return file.slice(start, end); } else if (file.webkitSlice) { ...

Is it possible to automatically submit a form upon page load?

I've tested a variety of scripts and jquery examples on this site, but none of them seem to fit my specific needs. My goal is to automatically submit a form without the user having to click the submit button. Once the page loads, the autosubmit func ...

Order of Execution for Nested Promises

Curious about nested promises, I came across this coding challenge in my tutorials. Can someone shed some light on the execution order of this code? new Promise((resolve) => { new Promise((res) => { console.log("c"); resolve(3); ...

Is it possible to transform a Lua table into a C array?

What I'm trying to find is something along the lines of: lua script MY_ARRAY = { 00, 10, 54, 32, 12, 31, 55, 43, 34, 65, 76, 34, 53, 78, 34, 93 } c code lua_Number array[] = lua_getarray("MY_ARRAY"); Does a solution for this exist? Is there a sim ...

Guide on sending a JSON object to an EJS javascript loop efficiently

Seeking assistance with passing a Json object named myVar to the home.ejs file below. How should I assign the value to the variable called data? <table id="example" class="table table-striped table-bordered dataTable" cellspacing="0" width="100%"> ...

Using Lodash library to iterate through a collection using the _.forEach method

Currently, I am attempting to implement the lodash forEach method within a structure where a nested function is being used to call a mongo database. var jobs = []; _.forEach(ids, function(id) { JobRequest.findByJobId(id, function(err, result) { ...

Showing a nested dataset with a condition: Is there a way to hide the information for products that do not meet the color condition?

I need help refining my code to only display the quantity of colors that are more than 10. Right now, it shows all colors, including those with less than 10. How can I adjust this? Thank you. Find my codesandbox here: https://codesandbox.io/s/products-0cc ...

Combining CSS, jQuery, and HTML into a single .html file would allow for seamless integration

I've been searching extensively, but I haven't been able to locate exactly what I need. My goal is to merge my jQuery and CSS into a single .html file, but I'm struggling to get the jQuery functionality to work. Although I have experience wi ...

employing JavaScript to present an image

Here is the image code I have: <img id="imgId" src="img/cart.png" style="display: none"/> After clicking a button, it triggers a JavaScript function to show the image document.getElementById("imgId").style.display = "inline" The image display ...

Angular2's change detection mechanism does not behave as anticipated after receiving a message from a Worker

Within my angular2 application, I encountered a rather intriguing scenario which I will simplify here. This is AppComponnet export class AppComponent { tabs: any = []; viewModes = [ { label: "List View"}, { label: "Tree View" }, ...

Step-by-step guide on incorporating edit, update, and discard functionalities within an Angular Material table component (mat-table)

I am currently working on implementing edit, update, and discard functions in an angular material table. While I have been able to successfully edit and update the table row wise, I am struggling with how to discard table rows. If you would like to see wh ...