0

I have a csv file :

name,number,level
Mike,b1,0
Tom,b2,0
.....

I want to construct something like:

    matrix: { 

   { name: 'Mike', number: 'b1', level: 0 }, 
   { name: 'Tom', number: 'b2', level: 0 }, 
         ....
    }

and I want to be able to extract the properties,for example matrix.name.

My problem is that I want to search later using ejs file by name for example.

George
  • 5,808
  • 15
  • 83
  • 160
  • 2
    Refer this question: http://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript – Oshadha Jun 10 '16 at 19:08
  • 1
    Just like everyone said [in your previous question](http://stackoverflow.com/questions/37752811/extract-fields-from-object), objects should not have duplicate keys. Perhaps you want an array of objects. `matrix = [ { name: 'Mike', number: 'b1', level: 0 }, { name: 'Tom', number: 'b2', level: 0 }, ... ]`. You might also consider using the names as a key for the rest of the properties (for example `matrix.Mike = { number: 'b1', level: 0 }`). – Mike Cluck Jun 10 '16 at 19:08
  • @MikeC:Yes,you are right..Ok,I liked also your idea.Can you let me know how is done?Thanks! – George Jun 10 '16 at 19:21
  • @George Which one? The array or using names as a key? – Mike Cluck Jun 10 '16 at 19:24
  • @MikeC:If you can provide both I will be grateful.Thanks – George Jun 10 '16 at 19:26
  • @George I need to make sure we aren't misunderstanding each other. The format you show in your latest edit can't be done because, again, objects are key value pairs. You probably want arrays like `name: [ ... ]`. The two suggestions I made would not give you that format. The first would give you something [like this](https://jsfiddle.net/rsszhxx5/) and the second would give you something [like this](https://jsfiddle.net/vq8brhLw/). – Mike Cluck Jun 10 '16 at 19:33
  • @MikeC:Sorry,I meant `matrix = [ { name: 'Mike', number: 'b1', level: 0 }, { name: 'Tom', number: 'b2', level: 0 }, ... ]` – George Jun 10 '16 at 19:37

1 Answers1

1

I'm going to assume you already have the CSV data loaded. If not, you can refer to this question on how to load that data into your application.

Going from there, I'm going to assume your data is stored in a variable called csv. What you need to do is first process each line of that data and split the values on commas. After that, it's as simple as creating a new object which each value and adding that object to an array.

var csv = 'name,number,level\n' +
          'Mike,b1,0\n' +
          'Tom,b2,0';

// Split the data into individual lines by splitting on the line break
var lines = csv.split('\n');

// I'm going to store the column names so I can use them to automatically get each value
// Note that I also removed the columns from the `lines` array so it won't be there when we go through it
var columns = lines.shift();

// Make sure we split on the commas
columns = columns.split(',');

// Create an array for us to store the objects in
var matrix = [];

// Next, we begin processing each line
for (var i = 0, len = lines.length; i < len; i++) {
  var line = lines[i];
  
  // Each value is separated by a comma. Split the line on those commas
  var values = line.split(',');
  
  // Now we create a new object that we'll store each value in
  var obj = {};
  
  // Remember that `columns` array? We're going to use that to generate our keys
  for (var j = 0, numOfColumns = columns.length; j < numOfColumns; j++) {
    // This is going to be 'name', 'number', and 'level'
    var column = columns[j];
    
    // Here is where we extract the matching value
    var value = values[j];
    
    // Now we add a new property to that new object using the `column` as the key
    // and the `value` as, well, the value
    obj[column] = value;
  }
  
  // The object has been generated, add it to the array
  matrix.push(obj);
}

// Display the complete array
document.querySelector('pre').innerText = JSON.stringify(matrix, null, 2);
<pre></pre>
Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • :Ok,nice!regarding the `using the names as a key for the rest of the properties (for example matrix.Mike = { number: 'b1', level: 0 })`? (upvoted) – George Jun 10 '16 at 20:33
  • @George It's not too hard to infer. You make `matrix` an object (`var matrix = {}`) and you do `matrix[values[0]] = obj`. `values[0]` will be the name so you'll just be adding a new property to `matrix` that matches that name. – Mike Cluck Jun 10 '16 at 20:35
  • :Ok,correct.And how can I use something like: `matrix.name` or `matrix.number` and give me the results? – George Jun 10 '16 at 20:39
  • @George You can't. That would imply that `matrix` has a property of `name`. I get the feeling you work with MatLab a lot or something. There's nothing in Javascript that would allow you to write `matrix.name` and get all of the names. The only way you could do that is if you created a property of `matrix` called `name` that contained each of the names in it. But that would (probably) *only* have the names, not the rest of the data. You need to decide **how** you want to access your data. This is becoming a bit of a running in circles thing. – Mike Cluck Jun 10 '16 at 20:47
  • :Hmm..My main problem as I state in the question is to extract for example the name field.I just want from the csv file to extract evry property.Because I want to be able to search according to this ,in a ejs file. ( I am beginner in javascript and this stuff you are right.I have worked with matlab :) ). – George Jun 10 '16 at 20:51
  • @George But you keep changing what you want! Just telling me you want to extract the data from the file doesn't mean anything. You need to have that data stored in some kind of structure. How you intend to use that data determines what kind of structure you should use. If you want to get the rest of the data associated with someone based on their name, go with the second option. Then `matrix[name]` will give you their information. You can get a list of all of the names from that format by getting the keys of `matrix`. `Object.keys(matrix)`. – Mike Cluck Jun 10 '16 at 20:53
  • :Sorry for the mess..:) – George Jun 10 '16 at 20:59