javascript split

Dear all,
the following code split a string by line "\n" :
var jsonfied = [{
		items : items.replace(/,$/, "").split("\n").map(function(name) {
			return {
				name : name
			};
		})
	}];

Open in new window

beside splitting on "\n" I need to split each line by ","
first col will titled : "name"
2'th col will titled : "column_type"
to generate following string :
 [
			{
				"name": "ID",
				"column_type": "typ1"
			},
			{
				"name": "class",
				"column_type": "typ2"
     		                 }
]

Open in new window

ethar1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Can you give a sample of the string with a couple of lines at least?
Just to be sure.
0
Pierre CorneliusCommented:
try this:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
	<textarea id="txtInput" cols="40" rows="5" >field1, typ1
field2, typ2</textarea>
	<input type="button" value="parse" onclick="parse();" />
	<textarea id="txtResult" cols="40" rows="5" ></textarea>

	<script type="text/javascript">
		function parse() {
			var tbInput = document.getElementById("txtInput");
			var tbResult = document.getElementById("txtResult");
			var s = tbInput.value.split("\n");
			tbResult.value='';
			var line=[];
			for (var i=0; i< s.length; i++)
			{
				line = s[i].split(",");
				tbResult.value += '{"name":"'+line[0].trim()+'", ' + '"column_type":"'+line[1].trim()+'"}';
				if (i<s.length-1)tbResult.value += ', ';
			}
			tbResult.value = '['+tbResult.value+']';
		}
	</script>
</script>
</body>
</html>

Open in new window

0
ethar1Author Commented:
PierreC thanks for your response,
somehow the result are:

[undefined
{"name":"FieldName01", "column_type":"typ1"},
 {"name":"FieldName02", "column_type":"typ2"},
 {"name":"FieldName03", "column_type":"typ3"}
]


instead of "undefined" I need to use : "TableName"
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Pierre CorneliusCommented:
The demo I gave expects the input string in a specific format i.e. fieldname, type[newline]
so if the input string is not in expected format it could result in undefined e.g. if trying to split a line using comma as separator but it doesn not have a comma, then refering to line[1] (i.e. the column_type) would result in undefined error.

What is the input string you are using?

You did not mention table name in your original question. Can you define a bit more your input requirements. E.g. first line of input string contains table name, subsequent lines contain field names. Can input string have more than one table? if so, you need some way of distinguishing that.
0
ethar1Author Commented:
FieldName01,typ1
FieldName02,typ2
FieldName03,typ3

I just need what I mentioned in first post,
no need for table name or anything...
also I preferred to use split + map function...
0
Pierre CorneliusCommented:
when I put that into my demo, I get:
[{"name":"FieldName01", "column_type":"typ1"}, {"name":"FieldName02", "column_type":"typ2"}, {"name":"FieldName03", "column_type":"typ3"}]

So not sure where you are getting the undefined from. Did you change any of the code?
0
Alexandre SimõesManager / Technology SpecialistCommented:
I guess this is what you want: http://jsfiddle.net/VM3jc/1/

var jsonfied = [{
		items : items.replace(/,$/, "").split("\n").map(function(item) {
                        var values = item.split(',');
			return {
				name : values[0],
                                column_type: values[1]
			};
		})
	}];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pierre CorneliusCommented:
to change using map:
<script type="text/javascript">
		function parse() {
			var tbInput = document.getElementById("txtInput");
			var tbResult = document.getElementById("txtResult");
			
			tbResult.value = tbInput.value.split("\n").map(function (line) { var s = line.split(","); return '{"name":"'+s[0]+'", "column_type":"'+s[1]+'"}'});
			tbResult.value = '['+tbResult.value+']';
		}
	</script>

Open in new window

0
ethar1Author Commented:
Thanks guys :) that is exactly what I want
0
ethar1Author Commented:
Dear all,
could you please continue answer the question at :
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28272402.html
0
ethar1Author Commented:
dear all,
could you please continue answer the question :
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28325794.html
0
ethar1Author Commented:
dear all,
could you please continue answer the question :
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28325842.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.