Page 1 of 1

Array count

Posted: Thu Feb 13, 2014 3:20 pm
by Ivan6481911

Hello!
I created service (screen in attachments) - http://www.goldbeltbyway.com/api/feed...

How can I get length of array? On responce mapping I use this code:

//tour_cluster
if (value.field_cluster_tour_nids) {

Code: Select all

 var itemCount = value.field_cluster_tour_nids.field_cluster_tour_nid.length; 
 console.log('itemCount : ' + itemCount); 
 for (var i = 0; i < itemCount; i++)  
 { 
     var tourID = value.field_cluster_tour_nids.field_cluster_tour_nid[i]; 
     console.log('tour: ' + tourID + ' cluster: ' + value.nid); 
     //fillTourClusterWithValues (tourID, value.nid); 
 } // end for 

}
For ID 108 count is 2, BUT..... for 107 is two also! I noticed, that for 107 element is STRING, not array. Why? What is the correct way? log in attachments.
Thanks!
Image Image


Array count

Posted: Thu Feb 13, 2014 4:52 pm
by Kateryna Grynko

Hi Ivan,

You should check if your variable is an object:
preif (typeof === "object")/pre
The returned length=2 is a string length.


Array count

Posted: Fri Feb 14, 2014 9:19 am
by Ivan6481911

Hi, Katya.
Thank you! I made check if "string". But I had doubts if it is right.


Array count

Posted: Fri Feb 14, 2014 11:49 am
by Maryna Brodina

Hello! Yes, you're right. Should be something like this preif (value&#46;field_cluster_tour_nids && value&#46;field_cluster_tour_nids&#46;field_cluster_tour_nid) {
if (typeof value&#46;field_cluster_tour_nids&#46;field_cluster_tour_nid === 'string') {
console&#46;log('itemCount : ' + itemCount);
console&#46;log('tour: ' + value&#46;field_cluster_tour_nids&#46;field_cluster_tour_nid + ' cluster: ' + value&#46;nid);
} else {
var itemCount = value&#46;field_cluster_tour_nids&#46;field_cluster_tour_nid&#46;length;
console&#46;log('itemCount : ' + itemCount);
for (var i = 0; i < itemCount; i++) {
var tourID = value&#46;field_cluster_tour_nids&#46;field_cluster_tour_nid;
console&#46;log('tour: ' + tourID + ' cluster: ' + value&#46;nid);
&#47;&#47;fillTourClusterWithValues (tourID, value&#46;nid);
} &#47;&#47; end for
}
} /pre