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

What is the best way to retrieve the chosen table row (tr) in this situation?

I have a table displayed below: How can I retrieve the selected mobile number of the tr when a button is clicked? <table class="table table-striped table-bordered table-hover table-full-width dataTable" id="sample_2" aria-describedby="sample_2_info"&g ...

Encountering a Selection Issue with IE8

I am encountering an issue with my script that involves a form with two select elements for region and state, allowing users to filter states based on the selected region. Everything works smoothly except for one problem - when testing in IE8, I receive th ...

NodeJS - leveraging forked processes on a single virtual machine with multiple processors versus the utilization of multiple virtual machines running a single process

In my NodeJS development project, I am working on a service that generates text files from images using a node wrapper for the tesseract OCR engine. The goal is to have this service running continuously, starting and restarting (in case of a crash) using u ...

Angular Controller encounters issue with event handler functionality ceasing

One of my Angular controllers has the following line. The event handler works when the initial page loads, but it stops working when navigating to a different item with the same controller and template. document.getElementById('item-details-v ...

JavaScript and HTML code for clipboard functionality without the need for Flash

My challenge lies in creating a grid with numerous columns and data. The user has expressed the desire for a copy to clipboard button that will allow them to easily copy the data. Can anyone suggest ways to implement Copy to Clipboard functionality withou ...

Discovering WebElements nested within other WebElements using WebdriverJS

Looking at this HTML structure <div> <div> <p class="my-element"></p> </div> <div> <div> <div> <p class="the-text"> I want to retrieve this text</p> </div> </div> <div> ...

Exploring the Differences: Module, Dependency, Library, Package, and Component

I'm finding myself a bit perplexed about the relationship between packages, modules, dependencies, and libraries. Are packages and modules considered dependencies? And can libraries be categorized as packages that are installed through various tools l ...

Retrieving data from a C# datatable in JSON format and presenting it in a jQuery datatable

Recently, I've been diving into Jquery Datatable and trying to work with it using a JSON string. However, despite my efforts over the past couple of days, I haven't been able to get the desired output. Here's a snippet of my HTML : <bo ...

Webpack loaders or plugins: understanding the distinction

Can you explain the distinction between loaders and plugins in webpack? I found on the documentation for plugins that it states: Plugins are used to incorporate extra functionalities usually related to bundles within webpack. While I understand that b ...

What causes a build to fail when package-lock.json is removed, but succeeds when running "npm install" without deleting it?

In a recent project, I decided to clean up some unnecessary libraries from my package.json file. After removing these libraries, deleting the node_modules folder, and running npm install, everything seemed to be working fine. However, when I tried to repe ...

Exploring Typescript Logging with Bunyan and Logentries

Looking to implement remote Logging using logentries.com for my ionic app. Snippet from my package.json: "dependencies": { "bunyan": "^1.8.5", "bunyan-logentries": "^1.2.0", }, "devDependencies": { "@types/bunyan": "0.0.35", "@typ ...

Issues with Sound Not Playing When Button is Clicked in Python Flask

My goal is to trigger an audio sound when the Delete button is clicked. I've created an external JavaScript file and successfully linked it with Flask. index.html <a href="/delete/{{todoitem.item_id}}" class="btn btn-danger" onclick="playDelSound ...

Tips for changing the focus between various modals and HTML pages after minimizing a modal with jQuery

I have been utilizing a plugin from to minimize a bootstrap modal within my angular application. The problem I am facing with this plugin is that whenever I minimize a modal, I am unable to interact with the html page, such as typing in input boxes or sc ...

Activate fullscreen mode in Krpano on desktop by clicking a button

Is there a way to activate fullscreen mode upon clicking a button? I believe I should use the code: krpano.set(fullscreen,true); Currently, I have an image within a slideshow that includes a play button overlay. Once the button is clicked, the slideshow ...

What could be the reason for receiving an undefined user ID when trying to pass it through my URL?

Currently, I am in the process of constructing a profile page and aiming to display authenticated user data on it. The API call functions correctly with the user's ID, and manually entering the ID into the URL on the front end also works. However, wh ...

What is the reason for sending a JSON string in an HTTP POST request instead of a JavaScript object in AngularJS

When sending data via a post request in AngularJS, the code may look like this: $http({ method: 'POST', url: url, data: inputParams, headers: headers }) .then(function succes(response) { successCallBack(response.data); } Here is h ...

Delay the v-alert display after an item is added to the array basket using setTimeout

here is my custom rightTableMenu template <template> <div> <h1 align="center">{{ title }}</h1> <v-alert type="info" icon="mdi-emoticon-sad" v-if="basketStatus"> Empty Basket, please add some to basket < ...

Can anyone recommend a reliable continuous integration pipeline for StrongLoop and GitHub integration?

How can you effectively develop websites using strongloop and github/bitbucket, ensuring smooth transitions from development to testing to production? I understand the key components of a successful workflow, but I'm interested in hearing about strat ...

What steps are involved in incorporating a machine learning algorithm from a Python file into a Django website?

I'm currently utilizing the random forest algorithm in Python to make predictions about college dropouts. The algorithm has been completed, and now I need to integrate the file into a website using Django. However, I am facing issues as the imported f ...

What is the best way to incorporate Vue Apollo into a Vue Vite project?

I'm currently working on integrating Vue Apollo into a Vite project using the composition API. Here is how my main.js file looks: import { createApp } from 'vue' import App from './App.vue' import * as apolloProvider from '../ ...