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

  • SEARCH
  • Home
  • 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 8503805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:51:24+00:00 2026-06-11T01:51:24+00:00

I can’t get my head around this one guys. I have a CSV file

  • 0

I can’t get my head around this one guys. I have a CSV file which I read from via AJAX, and I am creating an array from what is returned. My CSV file looks like this:

ID,name,address
0,john,123 fake st
1,bruce,123 fake st
2,john,124 fake st
3,fred,125 fake st
4,barry,126 fake st

I call it via an ajax function:

if (window.XMLHttpRequest) {
    var ajax = new XMLHttpRequest();
} else var ajax = new ActiveXObject("Microsoft.XMLHTTP");

function include(src) {
    ajax.open('GET', src, false);
    ajax.send(null);
    return ajax.responseText;
}

and loop through it like so:

var bsf = include('csv.csv');
// construct an array from the first line of the file
// and use that array to name the keys in all further arrays
var cols = bsf.split('\r\n');
var ln1 = cols[0].split(',');
// pull out each line from bsf and turn it into an array
var line = [];
var table = {};
var curRow = [];

for (i = 1; i < cols.length; i++) { // i = 1 so we can skip the 'title' csv line
    curRow = cols[i].split(',');

    for (j = 0; j < curRow.length; j++) {
        line[ln1[j]] = curRow[j];
    }

    table[curRow[0]] = line;
}
console.dir(table);

Instead of an object with one array for each row, I have 4 arrays that all contain the last row of the csv file. The nested for loop completes properly, and if I alert(line) before entering it into the table object, it returns the current line array correctly, but still does not assign that array to the object line.

Where I want

 table{
     0: [id: 0, name: 'john', address: '123 fake st'],
     1: [id: 1, name: 'bruce', address: '124 fake st'],
     ...}

I get

 table{
     4: [id: 4, name: 'barry', address: '126 fake st'],
     4: [id: 4, name: 'barry', address: '126 fake st'],
     etc.}

Any ideas? I get the feeling I am assigning them all correctly throughout the loop, but then on the last run through I am assigning them all wrongly and overwriting the correct ones.

  • 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-11T01:51:26+00:00Added an answer on June 11, 2026 at 1:51 am

    Your problem is that you only have one line array that you refill over and over again:

    for (j = 0; j < curRow.length; j++) {
        line[ln1[j]] = curRow[j];
    }
    

    and then you add that line array to the table in different places:

    table[curRow[0]] = line;
    

    The result is that each entry in table will be the last row from cols.

    You simply need to put different arrays into table with something like this:

    for (i = 1; i < cols.length; i++) {
        line   = [ ]; // <-------- This is the important part!
        curRow = cols[i].split(',');
        for (j = 0; j < curRow.length; j++) {
            line[ln1[j]] = curRow[j];
        }
        table[curRow[0]] = line;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can any one help me in sorting this out in sed/awk/perl Input file Start
Can any one tell, how to get the result of LINQ query contains group
Can't figure out how to do this in a pretty way : I have
Can someone help me with this scenario? *There is a button, which when tapped,
Does anyone know how can I replace this 2 symbol below from the string
Can find why i get this error can someone help? package Android.data; public class
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can anyone explain to me why this program: for(float i = -1; i <
I have a jquery bug and I've been looking for hours now, I can't

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.