Solved

dynamic push

Posted on 2013-12-09
10
127 Views
Last Modified: 2013-12-26
Dear all,
I got items like this:
var values = lines[i].split(itemSeperator);

Open in new window


instead of do this:
				item.languages.push({
					name : values[0],
					column_type : values[1],
					IsNull : values[2],
					column_default : values[3]
				});

Open in new window


I need to do this:
				item.languages.push({
					itemcount: (total item in the array )
					item1 : values[1],
					item2: values[2], ....
                                         itemn : values[n]
				});

Open in new window

0
Comment
Question by:ethar1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39706658
I am not sure what you are asking.  If what you want is the source for the number of items in the list then:
itemcount: (values.length)

Cd&
0
 

Author Comment

by:ethar1
ID: 39706682
how can generate item programmatically
based on items count
0
 

Author Comment

by:ethar1
ID: 39706694
instead of
name : values[0],

I want to use
[item#n] :  values[n],
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39706784
I think it should be something like:
var lang = {};
lang.itemcount = values.length;
for (var i = 0; i < lang.itemcount; i++) {
	lang['item' + (i+1)] = values[i];
};

item.languages.push(lang);

Open in new window


I didn't actually test it but if it doesn't work it shouldn't be far :)
0
 

Author Comment

by:ethar1
ID: 39706806
I need to do
var lang = {};
	lang.itemcount = values.length;
	for (var i = 0; i < lang.itemcount; i++) {
	'item' + i.toString() : values[i];
	};

Open in new window

0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39706868
You don't need the toString().  The concatenation will cause the integer to be re-typed to string.

Cd&
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39707007
But didn't my code work?
What's the problem?

item1, item2, itemN... are properties of the same object that contains the itemcount.
In my code I'm adding these properties dynamically:

lang['item' + (i+1)] = values[i];

Open in new window


This line adds a property to the lang object called: 'item' + (i+1)
and sets its value as:
values[i]

Open in new window


This was what I understood that you wanted...
0
 

Author Comment

by:ethar1
ID: 39707132
is this code add property  item1 : values[1] ??
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39707166
json objects store properties in an array.
Because of this you can get and set a property by calling:
var a = {};
a["prop1"] = 123;

alert(a["prop1"]);  // 123
alert(a.prop1);   // 123

Open in new window

0
 

Author Comment

by:ethar1
ID: 39738737
in this code
				item.languages.push({
					name : values[0],
					column_type : values[1],
					IsNull : values[2],
					column_default : values[3]
				}); 

Open in new window


its defined the columns name ( name , column_type ,IsNull,column_default ) for known array.


instead I need to push unknown array ( dynamic array)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
modify change color of text 9 48
sort Multi-dimensional array 6 42
XML loaded in a form with dropdown 6 50
How can I do this in Angular, instead of jQuery? 2 21
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question