Converting a string into a list extracted from a text document

Within a file, the data (list) is structured as follows:

[5,[5,[5,100,-200],200,-400],300,-500]

Upon reading this file in an angular application, the contents are converted into a string object like so:

"[5,[5,[5,100,-200],200,-400],300,-500]"

Is there a way to revert this string back to its original list format?

While there is one method to tackle a different problem, where the file contains data:

200
300
400
500

The string can be split using:

var newData = fileContent.split('\n');
desiredList = []
for(var z=0;z<newData.length;z++){
        desiredList.push(parseInt(newData[z]))
    }

This provides the desired list structure. However, for the initial question posed, is there an alternative solution?

Answer №1

By using JSON.parse("[5,[5,[5,100,-200],200,-400],300,-500]")
, you can transform it into a JavaScript object.

Answer №2

One way to utilize the eval() function is shown below:

var arr = "[10,[10,[10,500,-1000],1000,-2000],2000,-3000]";
var parsedArr = eval(arr);
console.log(parsedArr);

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

Create a dynamic animation using Angular to smoothly move a div element across the

I currently have a div with the following content: <div ng-style="{'left': PageMap.ColumnWrap.OverviewPanelLeft + 'px'}"></div> Whenever I press the right key, an event is triggered to change the PageMap.ColumnWrap.Overvie ...

What is the best way to retrieve an object using callback data with jQuery?

Using jquery with a servlet to fetch data from a database. The callback function provides raw information from the database. How can I append these values to select options in jsp? Retrive_country servlet code: String sql1 = "SELECT * FROM state WHERE co ...

Clear Vuex state upon page refresh

In my mutation, I am updating the state as follows: try { const response = await axios.put('http://localhost:3000/api/mobile/v3/expense/vouchers/form_refresh', sendForm, { headers: { Accept: 'application/json', 'C ...

The success callback is not triggered when making a JSONP request

I have a specific URL that returns JSON data when accessed through a browser or REST client. However, I am having trouble making the request using jQuery in my express server running over HTTPS. Despite receiving a successful response in the network debug ...

The source code in VS Code was not accurately linked

I'm currently facing an issue with running my angular2 project from vs code. Below are the contents of my tsconfig and launch.json files. tsconfig.json { "compilerOptions": { "declaration": false, "emitDecoratorMetadata": true, "experi ...

"Must not be currently employed" when using window.open in a basic React application

Let me share a simplified version of the webapp I'm currently developing. Whenever I run into an Uncaught Error: Should not already be working. while executing the window.open(...) line in the following code snippet: const sleep = milliseconds => ...

Implementing CSS styling within a JavaScript file

I have a vague memory of an easy way to incorporate CSS code within a JS file, but I can't recall the specific details. Simply inserting CSS code into a JS file doesn't seem to work, so there may be a need for comments or some other method. *No ...

What is the best way to incorporate expressions within the ng-messages directive?

If I have a form named "signupForm", and I want to disable the form in case it is invalid using the ng-disabled directive on a button, I would use ng-disabled="{{formName}}.$invalid"> (where formName contains the value signupForm) When I check the butt ...

The correct organization of angular files

Seeking advice on the optimal angular file structure for my upcoming project. It will be a single page application featuring a video feed on the main page, as well as specific post viewing pages. The goal is to provide users with login capabilities, conten ...

Typescript loading icon directive

Seeking to create an AngularJS directive in TypeScript that wraps each $http get request with a boolean parameter "isShow" to monitor the request status and dynamically show/hide the HTML element depending on it (without utilizing $scope or $watch). Any ...

the pause in execution before my function redirects to a different route

Currently, I am developing a page using nodeJs with express which is supposed to display a table. However, I encountered an issue with my variable "allMusique" that contains the data for my page. When trying to access it initially, there seems to be an err ...

Ways to differentiate between an angular element and a jQuery element

In order to implement a feature where clicking outside of a dropdown hides it within a directive, I have the following code: $(document).click(function(e) { var selector = $(e.target).closest('.time-selector'); if (!selector. ...

Dynamic Selection List Population in jqGrid

Using jqGrid 4.13.3 - free jqGrid In the Add form, there is a static input element and a select list element. The keyup ajax function is bound to the input element using dataEvents in the beforeInitData event. Once the Add form is displayed, entering a va ...

Display the current position of the caret in a field that cannot be edited

Can a virtual caret be displayed between two letter boundaries in HTML/CSS/JavaScript, for example in a regular div without using contenteditable=true? Imagine having the following: <div>Hello world</div> If I were to click between the "w" a ...

Tips for preventing a NodeJS script from crashing due to timeout being exceeded

Here is the issue I am encountering: I am attempting to scrape a website's content using NodeJS and puppeteer. Sometimes, my code halts with a Timeout Exceeded error. Is there a way for me to handle this timeout by implementing a function that will e ...

Utilizing ng-repeat to fetch and display an array of objects stored in Firebase

I am currently facing an issue while trying to access a list of notes from Firebase using AngularJS. I am unable to display the retrieved data even though there are no error messages appearing in the console. Notes.controller('ListGroupCtrl', ...

An error occurred while trying to add a property to an array because the object is not extensible: TypeError -

In my code, there is an object named curNode with the following structure: { "name": "CAMPAIGN", "attributes": {}, "children": [] } I am attempting to add a new node to the object like this: curNode!.children!.push({ name: newNodeName, ...

Navigate to the previous or next page using JavaScript

I've been struggling to figure out how to navigate one page up and down using simple links, but I haven't found a solution that works for me. The URL I am working with is: . When the "next page" button is clicked, it should go to ?page=2, and whe ...

Is there a way to determine if a user has interacted with a scrollbar on a website?

Is there a Jquery function available to determine if a user is currently holding onto a scrollbar on my website? I need to return a Boolean value based on whether the user has control of the scrollbar or not. Additionally, I am encountering a specific iss ...

Despite the status being 500, Chai is successfully navigating the test cases

I'm currently conducting test cases for my API using Chai, Mocha, and Chai HTTP. Even when I return a response of 500, my test case is still passing. Below is my test case: describe('/POST saveBatch', () => { it('it should save ...