首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Array.prototype.map()。索引未定义

Array.prototype.map()。索引未定义
EN

Stack Overflow用户
提问于 2017-05-16 04:03:45
回答 1查看 3.4K关注 0票数 3

我不知道错误的原因:

代码语言:javascript
复制
 { (questionList.length > 0) && questionList.map((item,index)
          =>
          <View key={index} style={styles.oneMonthV}>
            <Text
                style={[styles.textTGray, styles.fontTSize, TEXT_STYLE, styles.fontTQuestion]}>{item.content}</Text>
            <View style={styles.VTags}>
              {item.optionsList.map((item1, index1) =>
                  <TouchableOpacity key={index1}
                                    onPress={this._onPressButton.bind(this, index1)}>
                    <View>
                      { (item1.selected) ? (<TagCell modeColor='red'
                                                     content={item1.content}></TagCell>) : (
                          <TagCell
                              content={item1.content}></TagCell>) }
                    </View>
                  </TouchableOpacity>
              ) }
            </View>
          </View>
          )}

错误显示在

<View key={index} style={styles.oneMonthV}>。为什么索引没有定义。

我在chrome控制台中输入下面的代码是正确的。

代码语言:javascript
复制
['xxx','xx','xxx','www'].map((item,index) => {        console.log(`index==${index}`);  console.log(`item==${item}`);
    return (
        item + 'hahaha' )});

结果:

代码语言:javascript
复制
> index==0  
  item==xxx  
  index==1  
  item==xx  
  index==2  
  item==xxx  
  index==3  
  item==www  
(4) ["xxxhahaha", "xxhahaha", "xxxhahaha", "wwwhahaha"]

我认为我的代码是正确的。谁知道这个错误的原因?

因为这个错误太难了。我添加了这个问题的更多代码。

代码语言:javascript
复制
render() {
    const {questionList} = this.props;
    return (
        <ScrollView style={styles.container}>
          { (questionList.length > 0) && questionList.map((item,index) => (
          <View key={index} style={styles.oneMonthV}>
            <Text
                style={[styles.textTGray, styles.fontTSize, TEXT_STYLE, styles.fontTQuestion]}>{item.content}</Text>
            <View style={styles.VTags}>
              {item.optionsList.map((item1, index1) => (
                      <TouchableOpacity key={index1}
                                        onPress={this._onPressButton.bind(this, index1)}>
                        <View>
                          { (item1.selected) ? (<TagCell modeColor='red'
                                                         content={item1.content}></TagCell>) : (
                              <TagCell
                                  content={item1.content}></TagCell>) }
                        </View>
                      </TouchableOpacity>
                  )
              ) }
            </View>
          </View>
          )
          )}
        </ScrollView>
    );
  }

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-16 04:33:52

有几件事情出了问题,无论何时使用箭头函数,都应该从同一行的函数体开始。另外,在()中包装它也是一个很好的实践,您的检查也是不必要的,因为如果没有元素,它将不会映射,但是您必须检查undefined。还将{}更改为{}

代码语言:javascript
复制
{questionList && questionList.map((item,index) => (
      <View key={index} style={styles.oneMonthV}>
        <Text
            style={[styles.textTGray, styles.fontTSize, TEXT_STYLE, styles.fontTQuestion]}>{item.content}</Text>
        <View style={styles.VTags}>
          {item.optionsList.map((item1, index1) => (
              <TouchableOpacity key={index1}
                                onPress={this._onPressButton.bind(this, index1)}>
                <View>
                  { (item1.selected) ? (<TagCell modeColor='red'
                                                 content={item1.content}></TagCell>) : (
                      <TagCell
                          content={item1.content}></TagCell>) }
                </View>
              </TouchableOpacity>
              )
          ) }
        </View>
      </View>
    )
  )}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43992286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档