JavaScript equivalent code to C#'s File.ReadLines(filepath) would be reading a file line

Currently in my coding project using C#, I have incorporated the .NET package File.ReadLines(). Is there a way to replicate this functionality in JavaScript?

var csvArray = File.ReadLines(filePath).Select(x => x.Split(',')).ToArray();

I am aware that LINQ Select can be utilized in Javascript as well. My focus at the moment is on parsing a CSV file. Below is the snippet of my JS code.

    var fs = require('fs');
    var csv = require('fast-csv');
    var filepath = $('#appFilePathInput').value();

    fs.createReadStream(filepath)
    .pipe(csv())
    .on('data', function(data){
        //Should I implement the equivalent of LINQ Select and Split toArray here?
    });
    .on('data', function(data){
        console.log('Read Finished');
    });

The ultimate objective is to convert a local CSV file into an Array using pure JavaScript.

I would greatly appreciate any assistance in refining my current code, as I am new to writing code in JavaScript from scratch.

Thank you for your support!

Answer №1

To convert the new line character \n to a comma and then split by comma, you can use the following code:

var data = "Server1, Server2, Server3 \n 1.5, 2.9, 3.1"
var result = data.replace("\n", ",").split(",");

You can view the result in the console by visiting this fiddle https://jsfiddle.net/9oa25k2t/

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

"Attempting to connect to REST server using angularjs $resource. Unexpectedly, the success callback does not

After attempting to set up a REST server on Nodejs and accessing it from AngularJS using $resource for the first time, I have encountered some issues... In my controller, I am trying to load a JSON object (shopping cart) upon login for existing users or c ...

How can JavaScript onClick function receive both the name and value?

My current challenge involves a function designed to disable a group of checkboxes if they are not checked. Originally, this function was set to work onClick(), with one argument being passed from the checkbox element. Now, I need this function to be trigg ...

The NativeAppEventEmitter does not return any value

I've been grappling with obtaining a logged in user access token for quite some time. I initially faced challenges with it in JavaScript, so I switched to Objective-C and managed to succeed. Following that, I referred to this RN guide: https://facebo ...

Replicating JavaScript functions with the power of Ajax

I'm facing an issue with Bootstrap modal windows on my page. The modals are opening and closing successfully, but the content inside them is fetched through AJAX as HTML. For example, there's a button in the modal: <button id="myBtn"> and ...

Retrieve the value of a TextBox and display it as the title of a Tool

Hello there, I am currently learning front-end technologies and have a question. I would like to retrieve the value of a TextBox and display it in a Tool-tip. The code for the TextBox has a maximum length of 30 characters, but the area of the TextBox is no ...

Issues with Cross-origin resource sharing (CORS) arise when attempting to delete data using Angular

I am facing an issue with my Angular app (v1.13.15) and Express.js(v4.12.4) backend. Specifically, I have set up a DELETE method in my backend and enabled CORS support for it. However, every time I attempt to use the Angular $http.delete function, I enco ...

Implementing Ajax functionality in MVC 3 to return a partial view

I wanted to express my gratitude for this invaluable site that has taught me so much. Currently, I am working on an MVC3 component where I need to populate a selectlist and upon user selection, load a partial view with the relevant data displayed. Everythi ...

Dealing with Angular CORS Problems While Sending Successive Requests

I am currently working with Angular 6 and my backend consists of a node API. Occasionally, I encounter a CORS issue while making HTTP GET requests every 5 seconds. const url = 'https://<removed>.com/api/v1/transactions/' + transactionI ...

Having difficulty changing the visibility of a div element

I am currently working on a project that involves jQuery and ASP.Net. My main goal is to create a button that can hide/show a div using jQuery. Below is the code that I have implemented: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLI ...

I am encountering a strange issue when trying to redirect a URL in a Node.js application

I am currently working on developing a URL shortener using Node.js. I have successfully implemented a POST request that generates a random ID. This request requires a redirect URL input. However, I am facing an unusual issue when trying to create a GET ...

Issue with Browsersync causing task to malfunction in Gulp 4

Gulp Local v4.0.2, CLI v2.3.0 Browsersync v2.26.13 gulpfile.js: 'use strict' const gulp = require('gulp') const concat = require('gulp-concat') const babel = require('gulp-babel') const uglify ...

The slash character is escaped by the RegExp constructor, but the dot character is

Consider the following code: console.log(new RegExp('.git')); console.log(new RegExp('scripts/npm')); which produces the following output: /.git/ /scripts\/npm/ The puzzling question here is - why does it escape the slash in &a ...

Employing NPM to process SCSS, unfortunately, the script fails to automatically detect and apply changes, resulting in an error message

I have Node v12.10.0 and NPM v6.10.3 installed on my system. I even tried installing the LTS version of Node. In my project directory, I first ran "npm init" and then "npm install --save-dev node-sass". Everything seemed to work fine until this point. He ...

Tips for utilizing the /foo-:bar pathway in Nuxt.js?

I am trying to utilize the router /foo-:bar in Nuxt. Do you have any suggestions on how I could make this work? I attempted using pages/foo-_bar.vue but it did not yield the desired results. ...

Attempting to run the npm install command led to encountering a SyntaxError with an

Recently, I made some alterations to my npm configuration and ever since then, I have been consistently encountering the same error whenever I try to install various packages. It seems that due to a personal mistake in changing the npm settings, I am now u ...

Delete a filename in Internet Explorer with the power of JavaScript and jQuery

In my attempts to clear the file input field in IE using $('#files').val('');, I found that it was not effective. $('#uploadimgdiv').html(''); var fil1 = document.getElementById("files"); $('#fil1').val(&a ...

What is the process for displaying errors written to process.stderr in the AWS Lambda console?

There is a third-party library called Puppeteer that is logging errors to process.stderr, which is beyond my control. While my application is running in AWS Lambda, I need to log these errors for inspection purposes. However, process.stderr and process.st ...

Issue: The DLL initialization routine failed for electron, but it works perfectly fine on node.js

Currently, I am facing an issue while attempting to load a custom module in electron that is written in D using the node_dlang package. The module loads successfully with node, but encounters failures within electron. The test run with node, which works w ...

NodeJS: Resolving Dependencies based on Operating System

When working with the npm package email-templates, it's recommended to develop on OS X or Ubuntu/Linux. However, since we have team members using Windows, I started looking for an alternative to avoid dependency issues. That's when I came acros ...

Personalize the JSON output in a GraphQL query

Utilizing Express-js and the express GraphQL module, I have created my endpoint and web service; Currently, I am exploring methods to generate a customized response in GraphQL. My endpoint is quite straightforward: I'm retrieving books from the data ...