2016-11-17 76 views
2

我正在使用React Native创建应用程序,并且想要制作带节标题的<ListView>。我一直在关注几个不同的帖子,这些帖子描述了如何做到这一点,我遇到了一个奇怪的问题:阵列长度显示不正确

创建<ListView>的一部分是创建两个数组:一个是Sections的ID数组,另一个是由行ID组成的阵列数组。顶层的两个数组应该有相同数量的元素。

我创建从JSON数据阵列:

[ 
    { 
    "id": 12, 
    "name": "Surfhouse 1", 
    "guests": [ 
     { 
     "id": 100, 
     "name": "Oliver Nicholson", 
     "email": "[email protected]", 
     "age": "32" 
     }, 
     { 
     "id": 101, 
     "name": "Nollie Bollie", 
     "email": "[email protected]", 
     "age": "20" 
     }, 
     { 
     "id": 102, 
     "name": "Wootang", 
     "email": "[email protected]", 
     "age": "57" 
     } 
    ] 
    }, 
    { 
    "id": 25, 
    "name": "Surfhouse 2", 
    "guests": [ 
     { 
     "id": 200, 
     "name": "Alycia Woot", 
     "email": "[email protected]", 
     "age": "32" 
     }, 
     { 
     "id": 201, 
     "name": "Tim Jang", 
     "email": "[email protected]", 
     "age": "20" 
     }, 
     { 
     "id": 202, 
     "name": "Hootenany Yeah", 
     "email": "[email protected]", 
     "age": "57" 
     } 
    ] 
    } 
] 

,这反过来又被映射出用下面的函数(其中accommodation是导入JSON数据):

const formatDataForListView = accommodation => { 
     const dataBlob = {}; 
     const sectionIDs = []; 
     const rowIDs = []; 

     accommodation.forEach((accom) => { 
     sectionIDs.push(accom.id); 
     dataBlob[accom.id] = accom.name; 
     rowIDs[accom.id] = []; // initialise empty array associated with Section ID 
     accom.guests.forEach((guest) => { 
      rowIDs[accom.id].push(guest.id); 
      dataBlob[accom.id + ':' + guest.id] = guest; 
     }); 
     }); 

一切似乎很好:我得到了预期的阵列,但由于某种原因,RowIDs阵列有length: 26(我相信)它应该只是2.

enter image description here

rowIDs阵列的长度是怎样的26?

回答

1

因为您的索引值为12,索引值为25。这使得26的数组的length,因为它采用最大的索引,并添加一个长度,因为该数组是基于零。

如果您只想要两个项目,那么请使用索引01而不使用备件项目或使用带有密钥的对象。

为了更深入阅读:Relationship between length and numerical properties

+1

好吧,没错,疑难杂症。谢谢。 –

0

我建议的ROWID最好是对象。初始化一个数组的特定索引并不是一种好的做法,我们有对key => value的目的。如果你初始化一个数组中的特定索引,它会将所有先前的索引设置为undefined,这是不好的。使用实施对象将看起来像这样在普通的JavaScript -

var data = [ 
 
    { 
 
    "id": 12, 
 
    "name": "Surfhouse 1", 
 
    "guests": [ 
 
     { 
 
     "id": 100, 
 
     "name": "Oliver Nicholson", 
 
     "email": "[email protected]", 
 
     "age": "32" 
 
     }, 
 
     { 
 
     "id": 101, 
 
     "name": "Nollie Bollie", 
 
     "email": "[email protected]", 
 
     "age": "20" 
 
     }, 
 
     { 
 
     "id": 102, 
 
     "name": "Wootang", 
 
     "email": "[email protected]", 
 
     "age": "57" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "id": 25, 
 
    "name": "Surfhouse 2", 
 
    "guests": [ 
 
     { 
 
     "id": 200, 
 
     "name": "Alycia Woot", 
 
     "email": "[email protected]", 
 
     "age": "32" 
 
     }, 
 
     { 
 
     "id": 201, 
 
     "name": "Tim Jang", 
 
     "email": "[email protected]", 
 
     "age": "20" 
 
     }, 
 
     { 
 
     "id": 202, 
 
     "name": "Hootenany Yeah", 
 
     "email": "[email protected]", 
 
     "age": "57" 
 
     } 
 
    ] 
 
    } 
 
]; 
 

 
var rowIDs = {}; 
 
    
 
data.forEach(function(element) { 
 
    rowIDs[element.id] = []; 
 
    element.guests.forEach(function(guest) { 
 
     rowIDs[element.id].push(guest.id); 
 
    }); 
 
}); 
 
console.log(rowIDs);

+0

你死定了。谢谢 :-) –