Ivan6481911
Posts: 0
Joined: Tue Dec 10, 2013 11:25 am

Array count

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

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

Array count

Hi Ivan,

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

Ivan6481911
Posts: 0
Joined: Tue Dec 10, 2013 11:25 am

Array count

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

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

Array count

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

Return to “Issues”