美文网首页
react native 以state的某个值作为key\以数组

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

作者: 米开朗骑騾 | 来源:发表于2019-05-25 00:17 被阅读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>
        );
    }

相关文章

网友评论

      本文标题:react native 以state的某个值作为key\以数组

      本文链接:https://www.haomeiwen.com/subject/qubozqtx.html