• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

dynamic push

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
ethar1
Asked:
ethar1
  • 5
  • 3
  • 2
1 Solution
 
COBOLdinosaurCommented:
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
 
ethar1Author Commented:
how can generate item programmatically
based on items count
0
 
ethar1Author Commented:
instead of
name : values[0],

I want to use
[item#n] :  values[n],
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.

 
Alexandre SimõesManager / Technology SpecialistCommented:
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
 
ethar1Author Commented:
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
 
COBOLdinosaurCommented:
You don't need the toString().  The concatenation will cause the integer to be re-typed to string.

Cd&
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
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
 
ethar1Author Commented:
is this code add property  item1 : values[1] ??
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
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
 
ethar1Author Commented:
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
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.

Join & Write a Comment

Featured Post

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.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now