leonardo
Posts: 0
Joined: Thu Mar 14, 2013 7:04 pm

Simple Things Tripping

It's very frustrating sometimes not knowing how very simple things work. I'm trying to get the element count of the data in a portion of my RestService from an xml.
In JS of success I setup and could get results as so:

codewindow.nData=data.Main.xnode.xxnode;
window.introData=data.Main.intro;/code

Though nData and introData are both accessible, none of these would give the count of the nodes for iteration:
codevar count = data.introData.length;
trace(introData.length)/code
Both yielded "Undefined"
codealert($('[dsid=introData]').length);/code //gives 0

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Simple Things Tripping

If this works:

code
data.Main.intro
/code

are you sure this is the correct path?

code
data.introData.length;
/code

leonardo
Posts: 0
Joined: Thu Mar 14, 2013 7:04 pm

Simple Things Tripping

Thanks Max. I see what's happening now. So, my xml structure is like this:
code
<Main>
<intro>
<subintro>1<subintro>
<subintro>2<subintro>
<subintro>2<subintro>
<&#47;intro>
<groups>
<group>
<subgrp>1<subgrp>
<subgrp>2<subgrp>
<&#47;group>
<group>
<subgrp>1<subgrp>
<subgrp>2<subgrp>
<&#47;group>
<&#47;groups>
<&#47;Main>
/code
What is happening is that I cannot get data length for: codeMain&#46;groups&#46;group/code
But I cannot get data length for:
codeMain&#46;intro/code or for
codeMain&#46;groups/code
I need to be able to iterate through the children of "Main&#46;intro" for processing because the structure within it is more complicated than I represented here. Thanks.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Simple Things Tripping

Hello! You can get data.Main.groups.group.length on service success because data.Main.groups.group is an array.
But you can't get data.Main.intro.length and data.Main.groups.length because data.Main.intro and data.Main.groups are objects and do not have property length.

leonardo
Posts: 0
Joined: Thu Mar 14, 2013 7:04 pm

Simple Things Tripping

Thanks for the explanation. So how can I convert those to array as well, or get to iterate through their children?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Simple Things Tripping

Hi Leonardo,

http://api.jquery.com/jQuery.map/
http://api.jquery.com/jQuery.each/

These functions will help you to get around all the elements in the array and all the fields in the object.

leonardo
Posts: 0
Joined: Thu Mar 14, 2013 7:04 pm

Simple Things Tripping

Getting close, I think. I tried the examples, but got only one to work:
code$&#46;each(introData, function(index, value) {
alert(index + ': ' + value);
}); /code
But the results are in groups--all the items in are all grouped together, etc., and not in order in which they occur within the "object."

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Simple Things Tripping

Both $.map and $.each iterate only the top level items. The next levels of the object, you need to "bypass" separately.

The order of the elements in the object is not declared by JavaScript - the object is not an array.

leonardo
Posts: 0
Joined: Thu Mar 14, 2013 7:04 pm

Simple Things Tripping

I've been going in circles for the past two days over what's probably 5 or 6 lines of code and it's really frustrating. So, if what you recommended wouldn't get the desired result directly, could you please show me how to get the desired result?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Simple Things Tripping

What are the input parameters and what do you want to get as a result?

Return to “Issues”