Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:30:20+00:00 2026-06-09T04:30:20+00:00

What I’m trying to do is create a object literal on the fly with

  • 0

What I’m trying to do is create a object literal on the fly with dynamic properties.

I have an array containing the content (CSV format) of a file.

Each element is one line (the content was split with /\r?\n/).

The first element (first line) of that array contains the parameters I want to have for my object literal, separated by commas.

All the other elements are values (still CSV format) of the parameters set in the first line.

The code I have at the moment is the following :

function jsonDataArray(array) {
    var jsonResult = [],
        parameters = [],
        values;
    for(var i=0; i < array.length; i++) {
        if(i === 0) {
            var parameters = array[i].split(',');
            var objJSON = {};
            for(var k=0; k < parameters.length; k++) {
                objJSON.eval(parameters[k]);
            }
            continue;
        }
        values = array[i].split(',')
        for(var j=0; j < objJSON.length; j++) {
            objJSON[j] = values;
        }
        jsonResult.push(objJSON);
    }
    return jsonResult;
}

Now when I launch this code in node, the objJSON.eval(parameters[k]) line seems to be the one where the problem is, but I wasn’t able to solve the problem.

So basically my questions are the following :

  • How should I proceed to have the parameters from the first line set as parameters of a JSON object + fill the values of the other lines in ?

  • Is it safe to parse new lines with this : /\r?\n/ ?

Thank you in advance for your help !

EDIT : I mistakenly used the term JSON to mean object literal so I corrected the question. I didn’t modify the function though so that I don’t add errors in the code by mistake.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:30:21+00:00Added an answer on June 9, 2026 at 4:30 am

    It looks to me you want an array of objects, each object having keys from the first row (headers).

    The following code assumes you already broke the csv into each line via \r?\n

    (function() {
      var csv = [];
          csv.push('name,age,gender');
          csv.push('jake,13,m');
          csv.push('sarah,15,f');
          csv.push('john,18,m');
          csv.push('nick,13,m');
    
      console.log(csv);
    
      function jsonDataArray(array) {
        var headers = array[0].split(',');
        var jsonData = [];
        for ( var i = 1, length = array.length; i < length; i++ )
        {
          var row = array[i].split(',');
          var data = {};
          for ( var x = 0; x < row.length; x++ )
          {
            data[headers[x]] = row[x];
          }
          jsonData.push(data);
    
        }
    
        return jsonData;
      }
    
      console.log(jsonDataArray(csv));
    
    
    })();
    

    http://jsbin.com/igiwor/2/edit

    This will produce something like the follow:

    [{ name="jake",  age="13",  gender="m"},
    { name="sarah",  age="15",  gender="f"}, 
    { name="john",  age="18",  gender="m"},
    { name="nick"  age="13",  gender="m"}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.