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

使用{[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 (
            
                {JSON.stringify(arr1)}Welcome to React Native!
                {JSON.stringify(arr2)}To get started, edit App.js
                {instructions}
            
        );
    }

你可能感兴趣的:(react native 以state的某个值作为key\以数组某个值作为key)