react native 以state的某个值作为key\以数组

2019-05-25  本文已影响0人  米开朗骑騾

使用{[key] : value}形式即可

constructor(props) {
        super(props);
        this.state = {
            array:['z','x','c','v','b','n']
        }
    }

    render() {

        let str = 'abc';
        let arr = str.split('');
        let arr1 = [];
        for (let i = 0; i < arr.length; i++) {

            let dic = {[arr[i]]: i};
            arr1.push(dic)
        }
        let arr2 = [];
        for (let i = 0; i< this.state.array.length; i++){
            let dic = {[this.state.array[i]] : i}
            arr2.push(dic)
        }

        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>{JSON.stringify(arr1)}Welcome to React Native!</Text>
                <Text style={styles.instructions}>{JSON.stringify(arr2)}To get started, edit App.js</Text>
                <Text style={styles.instructions}>{instructions}</Text>
            </View>
        );
    }
上一篇下一篇

猜你喜欢

热点阅读