text to csv search function javascript -


I need to type some_name (text, key_name) which parses text in CSV format (quoted and avoided In values, values ​​should never return a function that has no commas or other special characters in the field values) function that sees a record by the value of the field specified in the form of the second argument of compile_csv_search. Assume that all the values ​​are unique in the main field

Sample Usage:

  var func_search = some_name ("ip, name, desc \ n" + "10.49.1.4, Server 1, Main Server \ n "+" 10.52.5.1, Server 2, Backup Server \ n "," Name "); console.log (func_search ("server2")); console.log (func_search ("server9"));   

... will print:

  {ip: "10.52.5.1", name: "server2", desc: "backup server"} < / code>  

I split the text into lines (\ n), then split the first row (,) and my result creates an array of keys that I return. But I do not know what to do next I have to use closures, but I do not know how. I would be happy to help everybody or suggest that this task will be led to resolve. What I have it:

  function compile_csv_search (text, key_name) {var results = {}; Var rows = text. Split ("\ n"); Var key_name = rows [0] .split (","); Lines.shift (); For example, return job search (name) {// "server2"} {/ * some code *}}}  for {var i = 0; i & lt; lines.length; i ++}  

Sorry for my wish.

For example ..

  var objs = {'data ': [],' ParseArray ': function (ar) {var properties = AR [0] .split (', '); For (var i = 1; i & lt; ar.length; i ++) {var obj = {}, vals = ar [i] .split (','); For (var j = 0; j & lt; vals.length; j ++) obj [properties [j]] = wal [ja]; this.data.push (obj); }}, 'SearchByProperty': function (property, search) {this.data.filter (function (AL) {returns el.hasOwnProperty (asset) & AL [property] == search;}); }} var strs = "\ ip, name, desc \ n \ 10.49.1.4, server1, main server \ n \ 10.52.5.1, server2, backup server" .split ("\ n"); objs.parseArray (STR); Console.log (objs.searchByProperty ('name', 'server2'))    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -