How can you choose a header in the json-2-csv Node tool?

Utilizing json-2-csv in a node application to update a specific value on row 1. Attempting to locate the Header "B" and swap out the value "2" with "7" using the following code:

    var C = "B"
    csvfile[itr].C = "7"

Here is the original csv file:

A, B, C

1, 2, 3

The resulting output is as follows:

A, B, C

1, 2, 7

The desired output should be:

A, B, C

1, 7, 3

Answer №1

Try using the header column name directly to set a new value in the CSV file without assigning it to a variable first, as shown below:

csvfile[itr]["B"] = "7"

Answer №2

There seems to be some confusion between .X and [X]. Remember, .X is equivalent to ['X'] (used for accessing property X), while you actually need [X] to access the property with a name from the variable X.

To clarify, what you require is either csvfile[itr][C] or simply csvfile[itr].B if you prefer eliminating the separate C variable altogether (this is similar to csvfile[itr]['B']).

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

Exploring and storing nested data structures in JavaScript/jQuery arrays

While I've come across various inquiries regarding the conversion of an array or object into a nested list, I have only managed to find one relevant question that addresses my specific issue. Despite attempting different methods to access the children ...

"What is the best way to use JavaScript to update several elements based on their class and incorporate distinct sub data for

Essentially, I am receiving a sequence of 5-10 events from an external server in the following format: event: add data: { "hostname":"name", "properties": {"info": "50"}} event: add data: { "hostname":"name2", "properties": {"info": "45"}} event: add da ...

Determining if a specific time falls within a certain range in JavaScript

I'm currently working on a Node.js application, but I have limited experience with Javascript. As part of my application, I need to implement validations for events and ensure that no two events can run simultaneously. To achieve this, I have set up ...

Creating dynamic ng-model for input type [text] in AngularJS

Here is the current situation I'm dealing with in my code: <form name="task_form" ng-app="myApp" ng-submit="tasksubmit()"> <ul class="items-list"> <li ng-repeat="task in taskslist ...

A guide on converting UTC time and date to local time and vice versa with MongoDB and Mongoose/Express

When comparing dates, I need to take into account my local Timezone (Asia/Kolkata GMT+5.30). var today = moment().toDate(); var tomorrow = moment().add(1,'days').toDate(); Moment : I am utilizing MomentJS for this task (but unsure if it's ...

Achieve immediate feedback without delaying for asynchronous function

As I develop an API to handle requests and execute a complex function using Puppeteer, I am faced with the challenge of not wanting the API to wait for the function to finish before sending a success response since it is primarily a submit API. The curren ...

I am facing issues with the executeJavaScript() function in htmlunit. It seems to be

Trying to log in using a JavaScript command, but even after using executeJavaScript(), the previous page content continues to display. The checkLogin(9143088043,123456,-1,false) command should connect to a new page. final WebClient webClient = ne ...

AngularJS Form Not Resetting After Submission

Currently, I am utilizing AngularJS on the frontend and the Play framework on the backend to handle incoming data. A persistent issue that I am encountering is that the form does not reset itself after successfully submitting the data upon clicking the su ...

Differences between importing Component and Vue from "vue-property-decorator" versus importing Vue from "vue"

Can you explain the specific differences and scenarios where importing Vue from vue-property-decorator versus just vue would be applicable? I have learned that when defining a custom component with a @Component decorator, it is necessary to import Vue fr ...

Is there a way to remove an individual file from an HTML file input field?

I recently took on the task of implementing a delete function for an HTML file input with the type "file". The goal is to delete a single file from a list of multiple files. While deleting all files at once is simple, I am having trouble with my function t ...

An error occurred: TypeError - Attempting to access a property that is undefined, even

I am struggling to implement default values when deleting a list from my todo list application. I want the current list info to temporarily use default values until the post request refreshes the available lists. Despite trying various methods to set defau ...

What is the process for requiring web workers in npm using require()?

I have a setup using npm and webpack, and a part of my application requires Web Workers. The traditional way to create web workers is by using the syntax: var worker = new Worker('path/to/external/js/file.js'); In my npm environment, this metho ...

Extract HTML data from the parent window of a URL without relying on an Internet Explorer browser - Tracking UPS information for multiple packages

Could I be heading in the wrong direction with my JavaScript code? I am new to using VBA for internet data retrieval and struggling to find a solution. Currently, I have a working function that utilizes IE.doc but it's slow and requires waiting for br ...

How can JavaScript routes be used to apply specific code to multiple pages on a website?

Can you provide guidance on how to run the same code for multiple pages using routes? Below is an example I am currently exploring: var routeManager = { _routes: {}, // Collection of routes add: function(urls, action) { urls.forEach(fun ...

Is there a built-in method or library for extracting data from JSON strings in programming languages?

Duplicate Query: how to parse json in javascript The data sent back by my server is in JSON format, but it doesn't follow the usual key/value pairs structure. Here's an example of the data I'm receiving: ["Value1","Value2","Value3"] ...

if considering an integer value of 0 as equivalent to null

I am struggling with using axios to send data to an API in react. Despite the server successfully receiving the values, my code is not entering the if block as expected. Interestingly, when I make the same request from a rest client, it works perfectly. He ...

Using only if-else statements and else-if conditions in JavaScript, arrange four numbers in order

How can I arrange 4 numbers in ascending order using only if-else and else-if statements in JavaScript without utilizing the sort function? ...

Transferring information from the main layout to subpages within Nextjs

I'm attempting to implement the following scenario; I have a file named /components/master_layout.js with the contents below: import useUser from "../data/use-user"; function MasterLayout({ children }) { const { data, error, mutate } ...

Filtering Array Values within an Object using JavaScript

I need help filtering an object based on array values. For example: { "sun":["sleep","walk"], "mon":["read","dance","ride"], "tue":["work",&q ...

Efficiently transferring components of a JavaScript project between files

For the first time, I am creating an npm package using ES6 and Babel. However, I am facing difficulties in connecting everything together so that it can be imported correctly by the end user. The structure of my build (output) folder is identical to src: ...