Page 1 of 2
Simple Things Tripping
Posted: Tue Jun 18, 2013 11:56 pm
by leonardo
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
Simple Things Tripping
Posted: Wed Jun 19, 2013 12:15 am
by maxkatz
If this works:
code
data.Main.intro
/code
are you sure this is the correct path?
code
data.introData.length;
/code
Simple Things Tripping
Posted: Thu Jun 20, 2013 1:13 am
by leonardo
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>
</intro>
<groups>
<group>
<subgrp>1<subgrp>
<subgrp>2<subgrp>
</group>
<group>
<subgrp>1<subgrp>
<subgrp>2<subgrp>
</group>
</groups>
</Main>
/code
What is happening is that I cannot get data length for: codeMain.groups.group/code
But I cannot get data length for:
codeMain.intro/code or for
codeMain.groups/code
I need to be able to iterate through the children of "Main.intro" for processing because the structure within it is more complicated than I represented here. Thanks.
Simple Things Tripping
Posted: Thu Jun 20, 2013 8:46 am
by Maryna Brodina
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.
Simple Things Tripping
Posted: Thu Jun 20, 2013 1:37 pm
by leonardo
Thanks for the explanation. So how can I convert those to array as well, or get to iterate through their children?
Simple Things Tripping
Posted: Thu Jun 20, 2013 3:57 pm
by Kateryna Grynko
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.
Simple Things Tripping
Posted: Thu Jun 20, 2013 6:10 pm
by leonardo
Getting close, I think. I tried the examples, but got only one to work:
code$.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."
Simple Things Tripping
Posted: Thu Jun 20, 2013 6:21 pm
by Kateryna Grynko
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.
Simple Things Tripping
Posted: Thu Jun 20, 2013 6:46 pm
by leonardo
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?
Simple Things Tripping
Posted: Thu Jun 20, 2013 7:20 pm
by Kateryna Grynko
What are the input parameters and what do you want to get as a result?