Creating an array of object from a string or an array

I need to create an array of objects from a string or an array.

I nitially had the attached function which is totally wrong.

The string I pass in is something like this: itemHeader="Engine,Browser,Platform,Version,Grade"

It needs to be output the following way:
{ "sTitle": "Engine" },      
{ "sTitle": "Browser" },      
{ "sTitle": "Platform" },      
{ "sTitle": "Version" },      
{ "sTitle": "Grade" },
                    
Thanks for any help you can provide
function createTableHeader(itemHeader)
{

    var items = itemHeader.split(",");

	var DTColumn = {};
	var DTCarray = [];
	
	for (x=0; x<items.length; x++)
	{
			DTColumn = "{\"sTitle\":"+" "+items[x]+"}";
			DTCarray.push(DTColumn.Title,);		
	}
	return DTCarray;	
}

Open in new window

LVL 5
IsaacSharePoint Client Side DeveloperAsked:
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.

leakim971PluritechnicianCommented:
test page : http://jsfiddle.net/2k1oyygb/
function createTableHeader(itemHeader) {
    return itemHeader.split(/[,"]/).slice(1,-1);
}

Open in new window

0
IsaacSharePoint Client Side DeveloperAuthor Commented:
I need to use it with the datatable jquery plugin, specifically for the 'aoColums'. It only takes an array of objects.  Will what you have work?
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
It does not play nice with strings
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

leakim971PluritechnicianCommented:
I don't know, just answered your main question, try it.
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
It does not work because it's a string.

My main question says

It needs to be output the following way:
{ "sTitle": "Engine" },      
{ "sTitle": "Browser" },      
{ "sTitle": "Platform" },      
{ "sTitle": "Version" },      
{ "sTitle": "Grade" },

Check out this page and click on "aaData", third from the top of the table.  As you will see, "aaColumns" accepts an array of objects which is what I need.  Not a string.

Any ideas?

Thanks!
0
leakim971PluritechnicianCommented:
function createTableHeader(itemHeader) {
    var strings = itemHeader.split(/[,"]/);
    var arr = [];
    for(var i=1;i<strings.length-1;i++) {
        var obj = { "sTitle" : strings[i] };
        arr.push(obj);
    }
    return arr;
}

Open in new window

0
IsaacSharePoint Client Side DeveloperAuthor Commented:
what's the difference between

 itemHeader.split(/[,"]/);

and

 itemHeader.split(",");
0
leakim971PluritechnicianCommented:
did it work?
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
No.

Object
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
0
leakim971PluritechnicianCommented:
The string I pass in is something like this: itemHeader="Engine,Browser,Platform,Version,Grade"

now I see :
var string = "Test,Ink,Test2,Test3,Test4";

anyway with the new string :
http://jsfiddle.net/ffjo9tcx/2/
0
leakim971PluritechnicianCommented:
var string = "Test,Ink,Test2,Test3,Test4";
alert(JSON.stringify(createTableHeader(string)));

function createTableHeader(itemHeader) {
    var strings = itemHeader.split(",");
    var arr = [];
    for(var i=0;i<strings.length;i++) {
        var obj = { "sTitle" : strings[i] };
        arr.push(obj);
    }
    return arr;
}

Open in new window

0
IsaacSharePoint Client Side DeveloperAuthor Commented:
It doesn't matter what is being passed in.  It will always be comma delimited.  Sorry for the confusion.

The results of arr will be eventually passed into the function below as tHeader.  It will be used for "aoColumns" parameter.

function CreateDataTable(tableID, sortCols, tHeader, tCells)
{

	var myDataTable = $(tableID).dataTable({
		"sScrollY": "auto",
		"bDestroy": true,
		"bPaginate": true,
		"bProcessing": true,
		"aaData": [tCells],
		"aoColumns": [tHeader],
		
		"sPaginationType": "full_numbers",
		"iDisplayLength": (defaultDrillDownLength || 10),
		"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
		"sDom": '<"topPagination"ilp>frt<"bottomPagination"p>',
		/*"aoColumnDefs": [
      		{ "bSortable": false, "aTargets": [ "non-sortable" ] }, {"bVisible":false, "aTargets": [ "filterable" ] }
    	],*/
    	"aaSorting": (sortCols ? sortCols : [[0,'asc']]),  
		"oLanguage": {
			"sInfoFiltered": ""
		},
		//turning bSortClasses off should be a performance boost for large datasets. disable the addition of the classes 'sorting_1', 'sorting_2' and 'sorting_3' to the columns which are currently being sorted on
		"bSortClasses": false
	});
}

Open in new window


I will try what you have and see if it works.
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
It did not work because it's a string and not an object.

Here's the alert:
object
Here's my table and as you can see, no headers bc it's a string
table
0
leakim971PluritechnicianCommented:
where are you seing a string?
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
jsonstringfy()
0
leakim971PluritechnicianCommented:
we have an alert too... are you going to let the alert too ?
jsonstringfy is to display the object
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
When I hard code the "aocolumns" like this
headersThe aocolumns is an array of objects

I get this:
table
0
leakim971PluritechnicianCommented:
function createTableHeader(itemHeader) {
    var strings = itemHeader.split(/[,"]/);
    var arr = [];
    for(var i=1;i<strings.length-1;i++) {
        var obj = { "sTitle" : strings[i] };
        arr.push(obj);
    }
    return arr;
}
var tHeader = createTableHeader("Engine,Browser,Platform,Version,Grade,Engine,Browser,Version,Grade,Grade");

Open in new window

and use this :
"aoColumns": tHeader,

Open in new window

not this :
"aoColumns": [tHeader],

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
IsaacSharePoint Client Side DeveloperAuthor Commented:
Here's the  value of tHeader:
tbl
Here's the code:
var myDataTable = $(tableID).dataTable({
		"sScrollY": "auto",
		"bDestroy": true,
		"bPaginate": true,
		"bProcessing": true,
		"aaData": tCells,
		"aoColumns": tHeader,
		/*"aoColumns": [    
	  				{ "sTitle": "Engine" },       
	  				{ "sTitle": "Browser" },       
	  				{ "sTitle": "Platform" },       
	  				{ "sTitle": "Version" },       
	  				{ "sTitle": "Grade" }, 
	  				{ "sTitle": "Engine" },       
	  				{ "sTitle": "Browser" },       
	  				{ "sTitle": "Platform" },       
	  				{ "sTitle": "Version" },       
	  				{ "sTitle": "Grade" },     
	  				{ "sTitle": "Grade" }  
	  			],*/
		"sPaginationType": "full_numbers",
		"iDisplayLength": (defaultDrillDownLength || 10),
		"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],

Open in new window


Here's the output:
tbl modal
0
leakim971PluritechnicianCommented:
use the hard code way too, setup a jsfiddle when you've time (with hard coded)
http://jsfiddle.net
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
Ok.  I took your advice and put it in jsfiddle and it worked so what you provided worked.  Thanks!

I will post another problem though and provide you the link to it on the same code.
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
Thanks for your patience.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.