React Native功能问题

时间:2016-12-18 19:54:28

标签: react-native

我正在尝试学习如何制作章节标题列表视图并按照https://medium.com/@darylrowland/reactnative-listview-with-section-headers-99a6714282c3#.t5fj6dswg中的教程进行操作。我是JS的新手,所以我一直坚持使用某个功能并且不要&# 39;不知道为什么它会继续说"某些线路上的意外令牌"。我已经尝试了4种不同的方式从许多教程中创建功能但仍然卡住了有人可以帮我修复它吗?

function ubahArrayMahasiswaJadiMap() {
  var kategoriMahasiswa = {}; //buat map kosong
  mahasiswa.forEach(mhs){  //source problem here after mhs
    if (!kategoriMahasiswa[mhs.sex]){
      //buat masukan pada map
      kategoriMahasiswa[mhs.sex] = [];
    }

    kategoriMahasiswa[mhs.sex].push(mhs);
  }

  return kategoriMahasiswa;
}

我已经尝试将其更改为

mahasiswa.forEach(mhs){
.....}

mahasiswa.forEach((mhs)){
    .....}

mahasiswa.forEach(function(mhs)){
    .....}

问题依然存在。

这是我的完整源代码

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

var mahasiswa = [
  {name: "Budi", sex : "male"},
  {name: "Ayu", sex : "female"},
  {name: "Tika", sex : "female"},
  {name: "Dara", sex : "female"},
  {name: "Abdul", sex : "male"},
  {name: "Jabbar", sex : "male"}
];

function ubahArrayMahasiswaJadiMap() {
  var kategoriMahasiswa = {}; //buat map kosong
  mahasiswa.forEach(mhs){ <------------------------------------- problem before the bracket
    if (!kategoriMahasiswa[mhs.sex]){
      //buat masukan pada map
      kategoriMahasiswa[mhs.sex] = [];
    }

    kategoriMahasiswa[mhs.sex].push(mhs);
  }

  return kategoriMahasiswa;
}

export default class ListLagi extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native Oke Dong !
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.android.js
        </Text>
        <Text style={styles.instructions}>
          Double tap R on your keyboard to reload,{'\n'}
          Shake or press menu button for dev menu
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('ListLagi', () => ListLagi);

1 个答案:

答案 0 :(得分:0)

查看你的forEach循环,并与本文档进行比较:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

您的语法错误,您的语法应该是:

mahasiswa.forEach((mhs) => {    
    // do this.
  });