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.

7
  • 2
    Refer this question: stackoverflow.com/questions/7431268/… Commented Jun 10, 2016 at 19:08
  • 1
    Just like everyone said in your previous question, 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 }). Commented Jun 10, 2016 at 19:08
  • @MikeC:Yes,you are right..Ok,I liked also your idea.Can you let me know how is done?Thanks! Commented Jun 10, 2016 at 19:21
  • @George Which one? The array or using names as a key? Commented Jun 10, 2016 at 19:24
  • @MikeC:If you can provide both I will be grateful.Thanks Commented Jun 10, 2016 at 19:26

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

7 Comments

: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 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.
:Ok,correct.And how can I use something like: matrix.name or matrix.number and give me the results?
@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.
: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 :) ).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.