在调用对象数组时,无法读取未定义的属性'teacherId'

时间:2018-02-05 13:03:18

标签: javascript jquery arrays

var teacherArray=[];

我创建了一个数组变量。 我正在创建一个包含键和值的数组。并将这些数据推送给老师。

   random={
    teacherId:TeacherId,
    day:day,
    periodCount:period,
    class:Studentclass,
    section:Studentsection,
    startTime:schoolStartTime,
    endTime:schoolEndTime 
};
teacherArray.push(random);
console.log(teacherArray);

在控制台中,我可以看到创建的数组。但是当点击提交按钮时,我正在调用数组

teacherLength=teacherArray.length;
    for (let k=0;k<=teacherLength;k++)
    {   
        var teachId=teacherArray[k].teacherId;
        console.log(teachId);
    }

在控制台中显示teacherId,但下一行显示错误

  

TypeError:无法读取未定义的属性'teacherId'   enter image description here

3 个答案:

答案 0 :(得分:2)

数组是基于零索引的。因此,当您编写k<=teacherLength时,您要求的数量超过数组的数量。那应该改为

k<teacherLength

答案 1 :(得分:1)

数组为0索引。你有,

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.itemSize = CGSizeMake((self.collectionView.frame.size.width/2)+100,self.collectionView.frame.size.height );

    //self.minimumInteritemSpacing = 10.0;
    self.minimumLineSpacing = 10.0;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.sectionInset = UIEdgeInsetsMake(0, 30.0, 0, 30.0);
}

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {

    NSInteger itemsCount = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:0];

    // Imitating paging behaviour
    //  previous offset and scroll direction
    if ((self.previousOffset > self.collectionView.contentOffset.x) && (velocity.x < 0.0f)) {
        self.currentPage = MAX(self.currentPage - 1, 0);

    } else if ((self.previousOffset < self.collectionView.contentOffset.x) && (velocity.x > 0.0f)) {
        self.currentPage = MIN(self.currentPage + 1, itemsCount - 1);
    }

    // Update offset by using item size + spacing
    CGFloat updatedOffset = (self.itemSize.width + 5) * self.currentPage;
    self.previousOffset = updatedOffset;

    return CGPointMake(updatedOffset, proposedContentOffset.y);
}

制作它,

for (let k=0;k<=teacherLength;k++)

答案 2 :(得分:1)

您的代码正在阅读 额外项目 ,您需要更换

self.yourView.setNeedsUpdateConstraints()

for (let k=0;k<=teacherLength;k++)