Convert from csv file to an array of specific columns chosen by column name












0














I am new to nodejs and try to write a program which read a csv file and can select data from csv_file by columns names and convert them into arrays.



I figured how to read the csv file and convert the columns into arrays(Thanks internet), but the columns are selected by the index number rather than the names.



code:



var csv = require('csv');
var csv_obj = csv();

function Column_data(signal_name, signal_type, initial_value, minimum, maximum ) {
this.signal_name = signal_name;
this.signal_type = signal_type;
this.initial_value = initial_value;
this.minimum = minimum;
this.maximum = maximum;
};

var csv_data = ;

csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
for (var row = 0; row < data.length; index++) {
csv_data.push(new Column_data(data[row][0], data[row][1], data[row][2], data[row][3], data[row][4]));
}
console.log(csv_data);
});


Here I'm using index values 1, 2, 3 to access columns 1, 2 and 3 in my csv. I want to be able to access columns by name rather than by index number because index can change.










share|improve this question




















  • 1




    What's the question?
    – gatsbyz
    Nov 20 at 16:25






  • 1




    how to do this; "From csv file to an array of specific columns chosen by column name." CODE: csv_data.push(new Column_data(data[index][0], data[index][1], data[index][2])); here I'm using 1, 2, 3 index values to access the columns 1, 2 and 3. I want to be able to access columns by name rather than the index numbers.
    – Rohandeep Singh
    Nov 20 at 16:26












  • @RohandeepSingh Is the csv you're requiring in your sample code an existing npm module, or is it one you've written? I ask because there is an existing package on npm called csv, and it already has the option to turn each row into an object keyed by column headers. If, however, csv is a module you've written, and you're asking how to get your module to do the same thing, that's a different question.
    – Tex
    Nov 20 at 17:51










  • @Tex The one I'm using is the pre-existing one. I will look into how I can use it. I was starting to think of other solutions as Gatsby mentioned there is no what of accessing columns by its name. I was thinks of reading just the first line from the csv and just indexing columns I need by finding where they exist in the the comma seperated values.
    – Rohandeep Singh
    Nov 20 at 18:33










  • @RohandeepSingh What confuses me is that the npm package named csv is not a function, so I can't tell which package you're actually using. If you tell us which package you're actually using, we may be able to help you figure out the correct configuration. It's also not difficult to do on your own, in case the package you've chosen to use doesn't support that feature out of the box.
    – Tex
    Nov 20 at 18:38


















0














I am new to nodejs and try to write a program which read a csv file and can select data from csv_file by columns names and convert them into arrays.



I figured how to read the csv file and convert the columns into arrays(Thanks internet), but the columns are selected by the index number rather than the names.



code:



var csv = require('csv');
var csv_obj = csv();

function Column_data(signal_name, signal_type, initial_value, minimum, maximum ) {
this.signal_name = signal_name;
this.signal_type = signal_type;
this.initial_value = initial_value;
this.minimum = minimum;
this.maximum = maximum;
};

var csv_data = ;

csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
for (var row = 0; row < data.length; index++) {
csv_data.push(new Column_data(data[row][0], data[row][1], data[row][2], data[row][3], data[row][4]));
}
console.log(csv_data);
});


Here I'm using index values 1, 2, 3 to access columns 1, 2 and 3 in my csv. I want to be able to access columns by name rather than by index number because index can change.










share|improve this question




















  • 1




    What's the question?
    – gatsbyz
    Nov 20 at 16:25






  • 1




    how to do this; "From csv file to an array of specific columns chosen by column name." CODE: csv_data.push(new Column_data(data[index][0], data[index][1], data[index][2])); here I'm using 1, 2, 3 index values to access the columns 1, 2 and 3. I want to be able to access columns by name rather than the index numbers.
    – Rohandeep Singh
    Nov 20 at 16:26












  • @RohandeepSingh Is the csv you're requiring in your sample code an existing npm module, or is it one you've written? I ask because there is an existing package on npm called csv, and it already has the option to turn each row into an object keyed by column headers. If, however, csv is a module you've written, and you're asking how to get your module to do the same thing, that's a different question.
    – Tex
    Nov 20 at 17:51










  • @Tex The one I'm using is the pre-existing one. I will look into how I can use it. I was starting to think of other solutions as Gatsby mentioned there is no what of accessing columns by its name. I was thinks of reading just the first line from the csv and just indexing columns I need by finding where they exist in the the comma seperated values.
    – Rohandeep Singh
    Nov 20 at 18:33










  • @RohandeepSingh What confuses me is that the npm package named csv is not a function, so I can't tell which package you're actually using. If you tell us which package you're actually using, we may be able to help you figure out the correct configuration. It's also not difficult to do on your own, in case the package you've chosen to use doesn't support that feature out of the box.
    – Tex
    Nov 20 at 18:38
















0












0








0







I am new to nodejs and try to write a program which read a csv file and can select data from csv_file by columns names and convert them into arrays.



I figured how to read the csv file and convert the columns into arrays(Thanks internet), but the columns are selected by the index number rather than the names.



code:



var csv = require('csv');
var csv_obj = csv();

function Column_data(signal_name, signal_type, initial_value, minimum, maximum ) {
this.signal_name = signal_name;
this.signal_type = signal_type;
this.initial_value = initial_value;
this.minimum = minimum;
this.maximum = maximum;
};

var csv_data = ;

csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
for (var row = 0; row < data.length; index++) {
csv_data.push(new Column_data(data[row][0], data[row][1], data[row][2], data[row][3], data[row][4]));
}
console.log(csv_data);
});


Here I'm using index values 1, 2, 3 to access columns 1, 2 and 3 in my csv. I want to be able to access columns by name rather than by index number because index can change.










share|improve this question















I am new to nodejs and try to write a program which read a csv file and can select data from csv_file by columns names and convert them into arrays.



I figured how to read the csv file and convert the columns into arrays(Thanks internet), but the columns are selected by the index number rather than the names.



code:



var csv = require('csv');
var csv_obj = csv();

function Column_data(signal_name, signal_type, initial_value, minimum, maximum ) {
this.signal_name = signal_name;
this.signal_type = signal_type;
this.initial_value = initial_value;
this.minimum = minimum;
this.maximum = maximum;
};

var csv_data = ;

csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
for (var row = 0; row < data.length; index++) {
csv_data.push(new Column_data(data[row][0], data[row][1], data[row][2], data[row][3], data[row][4]));
}
console.log(csv_data);
});


Here I'm using index values 1, 2, 3 to access columns 1, 2 and 3 in my csv. I want to be able to access columns by name rather than by index number because index can change.







javascript node.js typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 19:24









Heretic Monkey

6,32063365




6,32063365










asked Nov 20 at 16:18









Rohandeep Singh

247




247








  • 1




    What's the question?
    – gatsbyz
    Nov 20 at 16:25






  • 1




    how to do this; "From csv file to an array of specific columns chosen by column name." CODE: csv_data.push(new Column_data(data[index][0], data[index][1], data[index][2])); here I'm using 1, 2, 3 index values to access the columns 1, 2 and 3. I want to be able to access columns by name rather than the index numbers.
    – Rohandeep Singh
    Nov 20 at 16:26












  • @RohandeepSingh Is the csv you're requiring in your sample code an existing npm module, or is it one you've written? I ask because there is an existing package on npm called csv, and it already has the option to turn each row into an object keyed by column headers. If, however, csv is a module you've written, and you're asking how to get your module to do the same thing, that's a different question.
    – Tex
    Nov 20 at 17:51










  • @Tex The one I'm using is the pre-existing one. I will look into how I can use it. I was starting to think of other solutions as Gatsby mentioned there is no what of accessing columns by its name. I was thinks of reading just the first line from the csv and just indexing columns I need by finding where they exist in the the comma seperated values.
    – Rohandeep Singh
    Nov 20 at 18:33










  • @RohandeepSingh What confuses me is that the npm package named csv is not a function, so I can't tell which package you're actually using. If you tell us which package you're actually using, we may be able to help you figure out the correct configuration. It's also not difficult to do on your own, in case the package you've chosen to use doesn't support that feature out of the box.
    – Tex
    Nov 20 at 18:38
















  • 1




    What's the question?
    – gatsbyz
    Nov 20 at 16:25






  • 1




    how to do this; "From csv file to an array of specific columns chosen by column name." CODE: csv_data.push(new Column_data(data[index][0], data[index][1], data[index][2])); here I'm using 1, 2, 3 index values to access the columns 1, 2 and 3. I want to be able to access columns by name rather than the index numbers.
    – Rohandeep Singh
    Nov 20 at 16:26












  • @RohandeepSingh Is the csv you're requiring in your sample code an existing npm module, or is it one you've written? I ask because there is an existing package on npm called csv, and it already has the option to turn each row into an object keyed by column headers. If, however, csv is a module you've written, and you're asking how to get your module to do the same thing, that's a different question.
    – Tex
    Nov 20 at 17:51










  • @Tex The one I'm using is the pre-existing one. I will look into how I can use it. I was starting to think of other solutions as Gatsby mentioned there is no what of accessing columns by its name. I was thinks of reading just the first line from the csv and just indexing columns I need by finding where they exist in the the comma seperated values.
    – Rohandeep Singh
    Nov 20 at 18:33










  • @RohandeepSingh What confuses me is that the npm package named csv is not a function, so I can't tell which package you're actually using. If you tell us which package you're actually using, we may be able to help you figure out the correct configuration. It's also not difficult to do on your own, in case the package you've chosen to use doesn't support that feature out of the box.
    – Tex
    Nov 20 at 18:38










1




1




What's the question?
– gatsbyz
Nov 20 at 16:25




What's the question?
– gatsbyz
Nov 20 at 16:25




1




1




how to do this; "From csv file to an array of specific columns chosen by column name." CODE: csv_data.push(new Column_data(data[index][0], data[index][1], data[index][2])); here I'm using 1, 2, 3 index values to access the columns 1, 2 and 3. I want to be able to access columns by name rather than the index numbers.
– Rohandeep Singh
Nov 20 at 16:26






how to do this; "From csv file to an array of specific columns chosen by column name." CODE: csv_data.push(new Column_data(data[index][0], data[index][1], data[index][2])); here I'm using 1, 2, 3 index values to access the columns 1, 2 and 3. I want to be able to access columns by name rather than the index numbers.
– Rohandeep Singh
Nov 20 at 16:26














@RohandeepSingh Is the csv you're requiring in your sample code an existing npm module, or is it one you've written? I ask because there is an existing package on npm called csv, and it already has the option to turn each row into an object keyed by column headers. If, however, csv is a module you've written, and you're asking how to get your module to do the same thing, that's a different question.
– Tex
Nov 20 at 17:51




@RohandeepSingh Is the csv you're requiring in your sample code an existing npm module, or is it one you've written? I ask because there is an existing package on npm called csv, and it already has the option to turn each row into an object keyed by column headers. If, however, csv is a module you've written, and you're asking how to get your module to do the same thing, that's a different question.
– Tex
Nov 20 at 17:51












@Tex The one I'm using is the pre-existing one. I will look into how I can use it. I was starting to think of other solutions as Gatsby mentioned there is no what of accessing columns by its name. I was thinks of reading just the first line from the csv and just indexing columns I need by finding where they exist in the the comma seperated values.
– Rohandeep Singh
Nov 20 at 18:33




@Tex The one I'm using is the pre-existing one. I will look into how I can use it. I was starting to think of other solutions as Gatsby mentioned there is no what of accessing columns by its name. I was thinks of reading just the first line from the csv and just indexing columns I need by finding where they exist in the the comma seperated values.
– Rohandeep Singh
Nov 20 at 18:33












@RohandeepSingh What confuses me is that the npm package named csv is not a function, so I can't tell which package you're actually using. If you tell us which package you're actually using, we may be able to help you figure out the correct configuration. It's also not difficult to do on your own, in case the package you've chosen to use doesn't support that feature out of the box.
– Tex
Nov 20 at 18:38






@RohandeepSingh What confuses me is that the npm package named csv is not a function, so I can't tell which package you're actually using. If you tell us which package you're actually using, we may be able to help you figure out the correct configuration. It's also not difficult to do on your own, in case the package you've chosen to use doesn't support that feature out of the box.
– Tex
Nov 20 at 18:38














3 Answers
3






active

oldest

votes


















1














The following code gives you two options. One is using the 4.0.0 version of the csv library (npm install csv), the other is doing it by hand. Both return the same results given the sample input included below:



const csv = require('csv')

const data = `One,Two,Three
1,2,3
4,5,6`

// Using the csv library:
csv.parse(
data,
{ columns: true },
(err, result) => console.log(result)
)


// Doing it manually:
const rowToObject = (headers, cells) =>
headers.reduce(
(acc, header, i) => {
acc[header] = cells[i]
return acc
},
{}
)

const csvToObjects = file => {
const [headerRow, ...dataRows] = file.split('n')
const headers = headerRow.split(',')
return dataRows.map(
row => rowToObject(headers, row.split(','))
)
}

console.log(csvToObjects(data))

// both options output [{One:1,Two:2,Three:3},{One:4,Two:5,Three:6}]


You can see both of these running in this runkit - apologies, I'm not able to run StackOverflow snippets in my browser at this time.



Here I'll mention a third option: It seems you're using an older version of the csv package. In addition to csv().from.path(path).to.array(options, callback), the module also offers a to.object(options, callback) method, but I'm having trouble finding the documentation on the older version (and don't even know exactly which version you're currently using, which makes things even more difficult).






share|improve this answer























  • Are one two and three column names in the data.csv file or are they the names that you are assigning to columns in the program.
    – Rohandeep Singh
    Nov 20 at 19:18










  • And I really appreciate your effort in trying to help me. I could do this very easily in python, but I'm trying to learn some js. I hope you don't mind if some of my questions are too trivial.
    – Rohandeep Singh
    Nov 20 at 19:20












  • @RohandeepSingh One,Two,Three are taken from the first row of the csv input. Don't worry about asking simple questions. I may need to ask your help on some Python in the future ;) Make sure you read the update I just posted to my answer. It seems you're using an older version of the csv package. That version probably has a way to do what you want, but I haven't found the documentation for it.
    – Tex
    Nov 20 at 19:25












  • csv version is '6.4.1'. I think your solution might just do the trick. Now I'm thinking of what to use to open the CSV file. I found 'fs' is commonly used to read files, but the csv(); that I found specific for handling csv files doesn't seem to be working. It turns out csv() is not a thing apparently for this module, hence the syntax errors. I'll simply try using the 'fs' for finding the indexes, which is data = read the file using fs. Do you have a better suggestion in how I should handle the csv file.
    – Rohandeep Singh
    Nov 20 at 19:41












  • Pretty sure 6.4.1 is your npm version and not the version of csv. Use npm list csv to find the version of csv you have installed - my guess is 4.0.0. fs is the built-in node filesystem module, but I don't know what version of node you're on. You'll find plenty of fs-related questions here on SO, but feel free to ask if you get stuck.
    – Tex
    Nov 20 at 19:48





















0














There is no way to access the columns by the column_names. When the csv is read, it's just all comma-separated. Instead, you can change the csv output to be read by the column names, like -



csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
csv_data['signal_name'] = ;
csv_data['signal_type'] = ;
csv_data['initial_value'] = ;
for (var index = 1; index < data.length; index++) {
csv_data['signal_name'].push(data[index][0]);
csv_data['signal_type'].push(data[index][1]);
csv_data['initial_value'].push(data[index][2]);
...
}
console.log(csv_data);
});





share|improve this answer





















  • The issue here is that the index number can change. signal_name could be at index 3 in another file. That's the reason I'm looking to see if its possible to read the columns by name rather than the index numbers.
    – Rohandeep Singh
    Nov 20 at 16:38



















0














Try using Papaparse



and making a reader function like this first -



function reader (csv) {
return new Promise((resolve, reject) => {
if (csv) {
parser(csv, {
...config, // . this could be as you want it to - read the docs
complete:(response) => resolve(response.data),
error:(error) => reject(error),
})
}
})
}
contents = reader(csv)
csv_obj = contents.map((row) => ({
signal_name: row[0],
signal_type: row[1],
initial_value: row[2],
minimum: row[3],
maximum: row[4],

}))





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397204%2fconvert-from-csv-file-to-an-array-of-specific-columns-chosen-by-column-name%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The following code gives you two options. One is using the 4.0.0 version of the csv library (npm install csv), the other is doing it by hand. Both return the same results given the sample input included below:



    const csv = require('csv')

    const data = `One,Two,Three
    1,2,3
    4,5,6`

    // Using the csv library:
    csv.parse(
    data,
    { columns: true },
    (err, result) => console.log(result)
    )


    // Doing it manually:
    const rowToObject = (headers, cells) =>
    headers.reduce(
    (acc, header, i) => {
    acc[header] = cells[i]
    return acc
    },
    {}
    )

    const csvToObjects = file => {
    const [headerRow, ...dataRows] = file.split('n')
    const headers = headerRow.split(',')
    return dataRows.map(
    row => rowToObject(headers, row.split(','))
    )
    }

    console.log(csvToObjects(data))

    // both options output [{One:1,Two:2,Three:3},{One:4,Two:5,Three:6}]


    You can see both of these running in this runkit - apologies, I'm not able to run StackOverflow snippets in my browser at this time.



    Here I'll mention a third option: It seems you're using an older version of the csv package. In addition to csv().from.path(path).to.array(options, callback), the module also offers a to.object(options, callback) method, but I'm having trouble finding the documentation on the older version (and don't even know exactly which version you're currently using, which makes things even more difficult).






    share|improve this answer























    • Are one two and three column names in the data.csv file or are they the names that you are assigning to columns in the program.
      – Rohandeep Singh
      Nov 20 at 19:18










    • And I really appreciate your effort in trying to help me. I could do this very easily in python, but I'm trying to learn some js. I hope you don't mind if some of my questions are too trivial.
      – Rohandeep Singh
      Nov 20 at 19:20












    • @RohandeepSingh One,Two,Three are taken from the first row of the csv input. Don't worry about asking simple questions. I may need to ask your help on some Python in the future ;) Make sure you read the update I just posted to my answer. It seems you're using an older version of the csv package. That version probably has a way to do what you want, but I haven't found the documentation for it.
      – Tex
      Nov 20 at 19:25












    • csv version is '6.4.1'. I think your solution might just do the trick. Now I'm thinking of what to use to open the CSV file. I found 'fs' is commonly used to read files, but the csv(); that I found specific for handling csv files doesn't seem to be working. It turns out csv() is not a thing apparently for this module, hence the syntax errors. I'll simply try using the 'fs' for finding the indexes, which is data = read the file using fs. Do you have a better suggestion in how I should handle the csv file.
      – Rohandeep Singh
      Nov 20 at 19:41












    • Pretty sure 6.4.1 is your npm version and not the version of csv. Use npm list csv to find the version of csv you have installed - my guess is 4.0.0. fs is the built-in node filesystem module, but I don't know what version of node you're on. You'll find plenty of fs-related questions here on SO, but feel free to ask if you get stuck.
      – Tex
      Nov 20 at 19:48


















    1














    The following code gives you two options. One is using the 4.0.0 version of the csv library (npm install csv), the other is doing it by hand. Both return the same results given the sample input included below:



    const csv = require('csv')

    const data = `One,Two,Three
    1,2,3
    4,5,6`

    // Using the csv library:
    csv.parse(
    data,
    { columns: true },
    (err, result) => console.log(result)
    )


    // Doing it manually:
    const rowToObject = (headers, cells) =>
    headers.reduce(
    (acc, header, i) => {
    acc[header] = cells[i]
    return acc
    },
    {}
    )

    const csvToObjects = file => {
    const [headerRow, ...dataRows] = file.split('n')
    const headers = headerRow.split(',')
    return dataRows.map(
    row => rowToObject(headers, row.split(','))
    )
    }

    console.log(csvToObjects(data))

    // both options output [{One:1,Two:2,Three:3},{One:4,Two:5,Three:6}]


    You can see both of these running in this runkit - apologies, I'm not able to run StackOverflow snippets in my browser at this time.



    Here I'll mention a third option: It seems you're using an older version of the csv package. In addition to csv().from.path(path).to.array(options, callback), the module also offers a to.object(options, callback) method, but I'm having trouble finding the documentation on the older version (and don't even know exactly which version you're currently using, which makes things even more difficult).






    share|improve this answer























    • Are one two and three column names in the data.csv file or are they the names that you are assigning to columns in the program.
      – Rohandeep Singh
      Nov 20 at 19:18










    • And I really appreciate your effort in trying to help me. I could do this very easily in python, but I'm trying to learn some js. I hope you don't mind if some of my questions are too trivial.
      – Rohandeep Singh
      Nov 20 at 19:20












    • @RohandeepSingh One,Two,Three are taken from the first row of the csv input. Don't worry about asking simple questions. I may need to ask your help on some Python in the future ;) Make sure you read the update I just posted to my answer. It seems you're using an older version of the csv package. That version probably has a way to do what you want, but I haven't found the documentation for it.
      – Tex
      Nov 20 at 19:25












    • csv version is '6.4.1'. I think your solution might just do the trick. Now I'm thinking of what to use to open the CSV file. I found 'fs' is commonly used to read files, but the csv(); that I found specific for handling csv files doesn't seem to be working. It turns out csv() is not a thing apparently for this module, hence the syntax errors. I'll simply try using the 'fs' for finding the indexes, which is data = read the file using fs. Do you have a better suggestion in how I should handle the csv file.
      – Rohandeep Singh
      Nov 20 at 19:41












    • Pretty sure 6.4.1 is your npm version and not the version of csv. Use npm list csv to find the version of csv you have installed - my guess is 4.0.0. fs is the built-in node filesystem module, but I don't know what version of node you're on. You'll find plenty of fs-related questions here on SO, but feel free to ask if you get stuck.
      – Tex
      Nov 20 at 19:48
















    1












    1








    1






    The following code gives you two options. One is using the 4.0.0 version of the csv library (npm install csv), the other is doing it by hand. Both return the same results given the sample input included below:



    const csv = require('csv')

    const data = `One,Two,Three
    1,2,3
    4,5,6`

    // Using the csv library:
    csv.parse(
    data,
    { columns: true },
    (err, result) => console.log(result)
    )


    // Doing it manually:
    const rowToObject = (headers, cells) =>
    headers.reduce(
    (acc, header, i) => {
    acc[header] = cells[i]
    return acc
    },
    {}
    )

    const csvToObjects = file => {
    const [headerRow, ...dataRows] = file.split('n')
    const headers = headerRow.split(',')
    return dataRows.map(
    row => rowToObject(headers, row.split(','))
    )
    }

    console.log(csvToObjects(data))

    // both options output [{One:1,Two:2,Three:3},{One:4,Two:5,Three:6}]


    You can see both of these running in this runkit - apologies, I'm not able to run StackOverflow snippets in my browser at this time.



    Here I'll mention a third option: It seems you're using an older version of the csv package. In addition to csv().from.path(path).to.array(options, callback), the module also offers a to.object(options, callback) method, but I'm having trouble finding the documentation on the older version (and don't even know exactly which version you're currently using, which makes things even more difficult).






    share|improve this answer














    The following code gives you two options. One is using the 4.0.0 version of the csv library (npm install csv), the other is doing it by hand. Both return the same results given the sample input included below:



    const csv = require('csv')

    const data = `One,Two,Three
    1,2,3
    4,5,6`

    // Using the csv library:
    csv.parse(
    data,
    { columns: true },
    (err, result) => console.log(result)
    )


    // Doing it manually:
    const rowToObject = (headers, cells) =>
    headers.reduce(
    (acc, header, i) => {
    acc[header] = cells[i]
    return acc
    },
    {}
    )

    const csvToObjects = file => {
    const [headerRow, ...dataRows] = file.split('n')
    const headers = headerRow.split(',')
    return dataRows.map(
    row => rowToObject(headers, row.split(','))
    )
    }

    console.log(csvToObjects(data))

    // both options output [{One:1,Two:2,Three:3},{One:4,Two:5,Three:6}]


    You can see both of these running in this runkit - apologies, I'm not able to run StackOverflow snippets in my browser at this time.



    Here I'll mention a third option: It seems you're using an older version of the csv package. In addition to csv().from.path(path).to.array(options, callback), the module also offers a to.object(options, callback) method, but I'm having trouble finding the documentation on the older version (and don't even know exactly which version you're currently using, which makes things even more difficult).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 19:58

























    answered Nov 20 at 19:03









    Tex

    1,6031527




    1,6031527












    • Are one two and three column names in the data.csv file or are they the names that you are assigning to columns in the program.
      – Rohandeep Singh
      Nov 20 at 19:18










    • And I really appreciate your effort in trying to help me. I could do this very easily in python, but I'm trying to learn some js. I hope you don't mind if some of my questions are too trivial.
      – Rohandeep Singh
      Nov 20 at 19:20












    • @RohandeepSingh One,Two,Three are taken from the first row of the csv input. Don't worry about asking simple questions. I may need to ask your help on some Python in the future ;) Make sure you read the update I just posted to my answer. It seems you're using an older version of the csv package. That version probably has a way to do what you want, but I haven't found the documentation for it.
      – Tex
      Nov 20 at 19:25












    • csv version is '6.4.1'. I think your solution might just do the trick. Now I'm thinking of what to use to open the CSV file. I found 'fs' is commonly used to read files, but the csv(); that I found specific for handling csv files doesn't seem to be working. It turns out csv() is not a thing apparently for this module, hence the syntax errors. I'll simply try using the 'fs' for finding the indexes, which is data = read the file using fs. Do you have a better suggestion in how I should handle the csv file.
      – Rohandeep Singh
      Nov 20 at 19:41












    • Pretty sure 6.4.1 is your npm version and not the version of csv. Use npm list csv to find the version of csv you have installed - my guess is 4.0.0. fs is the built-in node filesystem module, but I don't know what version of node you're on. You'll find plenty of fs-related questions here on SO, but feel free to ask if you get stuck.
      – Tex
      Nov 20 at 19:48




















    • Are one two and three column names in the data.csv file or are they the names that you are assigning to columns in the program.
      – Rohandeep Singh
      Nov 20 at 19:18










    • And I really appreciate your effort in trying to help me. I could do this very easily in python, but I'm trying to learn some js. I hope you don't mind if some of my questions are too trivial.
      – Rohandeep Singh
      Nov 20 at 19:20












    • @RohandeepSingh One,Two,Three are taken from the first row of the csv input. Don't worry about asking simple questions. I may need to ask your help on some Python in the future ;) Make sure you read the update I just posted to my answer. It seems you're using an older version of the csv package. That version probably has a way to do what you want, but I haven't found the documentation for it.
      – Tex
      Nov 20 at 19:25












    • csv version is '6.4.1'. I think your solution might just do the trick. Now I'm thinking of what to use to open the CSV file. I found 'fs' is commonly used to read files, but the csv(); that I found specific for handling csv files doesn't seem to be working. It turns out csv() is not a thing apparently for this module, hence the syntax errors. I'll simply try using the 'fs' for finding the indexes, which is data = read the file using fs. Do you have a better suggestion in how I should handle the csv file.
      – Rohandeep Singh
      Nov 20 at 19:41












    • Pretty sure 6.4.1 is your npm version and not the version of csv. Use npm list csv to find the version of csv you have installed - my guess is 4.0.0. fs is the built-in node filesystem module, but I don't know what version of node you're on. You'll find plenty of fs-related questions here on SO, but feel free to ask if you get stuck.
      – Tex
      Nov 20 at 19:48


















    Are one two and three column names in the data.csv file or are they the names that you are assigning to columns in the program.
    – Rohandeep Singh
    Nov 20 at 19:18




    Are one two and three column names in the data.csv file or are they the names that you are assigning to columns in the program.
    – Rohandeep Singh
    Nov 20 at 19:18












    And I really appreciate your effort in trying to help me. I could do this very easily in python, but I'm trying to learn some js. I hope you don't mind if some of my questions are too trivial.
    – Rohandeep Singh
    Nov 20 at 19:20






    And I really appreciate your effort in trying to help me. I could do this very easily in python, but I'm trying to learn some js. I hope you don't mind if some of my questions are too trivial.
    – Rohandeep Singh
    Nov 20 at 19:20














    @RohandeepSingh One,Two,Three are taken from the first row of the csv input. Don't worry about asking simple questions. I may need to ask your help on some Python in the future ;) Make sure you read the update I just posted to my answer. It seems you're using an older version of the csv package. That version probably has a way to do what you want, but I haven't found the documentation for it.
    – Tex
    Nov 20 at 19:25






    @RohandeepSingh One,Two,Three are taken from the first row of the csv input. Don't worry about asking simple questions. I may need to ask your help on some Python in the future ;) Make sure you read the update I just posted to my answer. It seems you're using an older version of the csv package. That version probably has a way to do what you want, but I haven't found the documentation for it.
    – Tex
    Nov 20 at 19:25














    csv version is '6.4.1'. I think your solution might just do the trick. Now I'm thinking of what to use to open the CSV file. I found 'fs' is commonly used to read files, but the csv(); that I found specific for handling csv files doesn't seem to be working. It turns out csv() is not a thing apparently for this module, hence the syntax errors. I'll simply try using the 'fs' for finding the indexes, which is data = read the file using fs. Do you have a better suggestion in how I should handle the csv file.
    – Rohandeep Singh
    Nov 20 at 19:41






    csv version is '6.4.1'. I think your solution might just do the trick. Now I'm thinking of what to use to open the CSV file. I found 'fs' is commonly used to read files, but the csv(); that I found specific for handling csv files doesn't seem to be working. It turns out csv() is not a thing apparently for this module, hence the syntax errors. I'll simply try using the 'fs' for finding the indexes, which is data = read the file using fs. Do you have a better suggestion in how I should handle the csv file.
    – Rohandeep Singh
    Nov 20 at 19:41














    Pretty sure 6.4.1 is your npm version and not the version of csv. Use npm list csv to find the version of csv you have installed - my guess is 4.0.0. fs is the built-in node filesystem module, but I don't know what version of node you're on. You'll find plenty of fs-related questions here on SO, but feel free to ask if you get stuck.
    – Tex
    Nov 20 at 19:48






    Pretty sure 6.4.1 is your npm version and not the version of csv. Use npm list csv to find the version of csv you have installed - my guess is 4.0.0. fs is the built-in node filesystem module, but I don't know what version of node you're on. You'll find plenty of fs-related questions here on SO, but feel free to ask if you get stuck.
    – Tex
    Nov 20 at 19:48















    0














    There is no way to access the columns by the column_names. When the csv is read, it's just all comma-separated. Instead, you can change the csv output to be read by the column names, like -



    csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
    csv_data['signal_name'] = ;
    csv_data['signal_type'] = ;
    csv_data['initial_value'] = ;
    for (var index = 1; index < data.length; index++) {
    csv_data['signal_name'].push(data[index][0]);
    csv_data['signal_type'].push(data[index][1]);
    csv_data['initial_value'].push(data[index][2]);
    ...
    }
    console.log(csv_data);
    });





    share|improve this answer





















    • The issue here is that the index number can change. signal_name could be at index 3 in another file. That's the reason I'm looking to see if its possible to read the columns by name rather than the index numbers.
      – Rohandeep Singh
      Nov 20 at 16:38
















    0














    There is no way to access the columns by the column_names. When the csv is read, it's just all comma-separated. Instead, you can change the csv output to be read by the column names, like -



    csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
    csv_data['signal_name'] = ;
    csv_data['signal_type'] = ;
    csv_data['initial_value'] = ;
    for (var index = 1; index < data.length; index++) {
    csv_data['signal_name'].push(data[index][0]);
    csv_data['signal_type'].push(data[index][1]);
    csv_data['initial_value'].push(data[index][2]);
    ...
    }
    console.log(csv_data);
    });





    share|improve this answer





















    • The issue here is that the index number can change. signal_name could be at index 3 in another file. That's the reason I'm looking to see if its possible to read the columns by name rather than the index numbers.
      – Rohandeep Singh
      Nov 20 at 16:38














    0












    0








    0






    There is no way to access the columns by the column_names. When the csv is read, it's just all comma-separated. Instead, you can change the csv output to be read by the column names, like -



    csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
    csv_data['signal_name'] = ;
    csv_data['signal_type'] = ;
    csv_data['initial_value'] = ;
    for (var index = 1; index < data.length; index++) {
    csv_data['signal_name'].push(data[index][0]);
    csv_data['signal_type'].push(data[index][1]);
    csv_data['initial_value'].push(data[index][2]);
    ...
    }
    console.log(csv_data);
    });





    share|improve this answer












    There is no way to access the columns by the column_names. When the csv is read, it's just all comma-separated. Instead, you can change the csv output to be read by the column names, like -



    csv_obj.from.path('../data_files/Signals_Info.csv).to.array(function (data) {
    csv_data['signal_name'] = ;
    csv_data['signal_type'] = ;
    csv_data['initial_value'] = ;
    for (var index = 1; index < data.length; index++) {
    csv_data['signal_name'].push(data[index][0]);
    csv_data['signal_type'].push(data[index][1]);
    csv_data['initial_value'].push(data[index][2]);
    ...
    }
    console.log(csv_data);
    });






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 at 16:32









    gatsbyz

    195317




    195317












    • The issue here is that the index number can change. signal_name could be at index 3 in another file. That's the reason I'm looking to see if its possible to read the columns by name rather than the index numbers.
      – Rohandeep Singh
      Nov 20 at 16:38


















    • The issue here is that the index number can change. signal_name could be at index 3 in another file. That's the reason I'm looking to see if its possible to read the columns by name rather than the index numbers.
      – Rohandeep Singh
      Nov 20 at 16:38
















    The issue here is that the index number can change. signal_name could be at index 3 in another file. That's the reason I'm looking to see if its possible to read the columns by name rather than the index numbers.
    – Rohandeep Singh
    Nov 20 at 16:38




    The issue here is that the index number can change. signal_name could be at index 3 in another file. That's the reason I'm looking to see if its possible to read the columns by name rather than the index numbers.
    – Rohandeep Singh
    Nov 20 at 16:38











    0














    Try using Papaparse



    and making a reader function like this first -



    function reader (csv) {
    return new Promise((resolve, reject) => {
    if (csv) {
    parser(csv, {
    ...config, // . this could be as you want it to - read the docs
    complete:(response) => resolve(response.data),
    error:(error) => reject(error),
    })
    }
    })
    }
    contents = reader(csv)
    csv_obj = contents.map((row) => ({
    signal_name: row[0],
    signal_type: row[1],
    initial_value: row[2],
    minimum: row[3],
    maximum: row[4],

    }))





    share|improve this answer


























      0














      Try using Papaparse



      and making a reader function like this first -



      function reader (csv) {
      return new Promise((resolve, reject) => {
      if (csv) {
      parser(csv, {
      ...config, // . this could be as you want it to - read the docs
      complete:(response) => resolve(response.data),
      error:(error) => reject(error),
      })
      }
      })
      }
      contents = reader(csv)
      csv_obj = contents.map((row) => ({
      signal_name: row[0],
      signal_type: row[1],
      initial_value: row[2],
      minimum: row[3],
      maximum: row[4],

      }))





      share|improve this answer
























        0












        0








        0






        Try using Papaparse



        and making a reader function like this first -



        function reader (csv) {
        return new Promise((resolve, reject) => {
        if (csv) {
        parser(csv, {
        ...config, // . this could be as you want it to - read the docs
        complete:(response) => resolve(response.data),
        error:(error) => reject(error),
        })
        }
        })
        }
        contents = reader(csv)
        csv_obj = contents.map((row) => ({
        signal_name: row[0],
        signal_type: row[1],
        initial_value: row[2],
        minimum: row[3],
        maximum: row[4],

        }))





        share|improve this answer












        Try using Papaparse



        and making a reader function like this first -



        function reader (csv) {
        return new Promise((resolve, reject) => {
        if (csv) {
        parser(csv, {
        ...config, // . this could be as you want it to - read the docs
        complete:(response) => resolve(response.data),
        error:(error) => reject(error),
        })
        }
        })
        }
        contents = reader(csv)
        csv_obj = contents.map((row) => ({
        signal_name: row[0],
        signal_type: row[1],
        initial_value: row[2],
        minimum: row[3],
        maximum: row[4],

        }))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 17:26









        julian joseph

        7711




        7711






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397204%2fconvert-from-csv-file-to-an-array-of-specific-columns-chosen-by-column-name%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft