Javascript |反应原生地图功能无法正常工作
问题描述:
所以,我正在与React Native合作,发现了一个非常愚蠢的问题,我似乎无法摆脱困境。我已经用一些硬编码值声明了一个对象数组(这里是storeVar)。当我尝试使用JavaScript映射函数来循环使用它时,我只在第一个索引处获得值。代码如下 -Javascript |反应原生地图功能无法正常工作
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
ScrollView,
AsyncStorage,
TouchableOpacity
} from 'react-native';
import Note from './note.js'
// -------------------------------------------------------------------------
const storeVar = [
{
'd' : '',
'n' : 'Hello'
},
{
'd' : '',
'n' : 'Awesome'
},
];
export default class reactNativePractise extends Component {
constructor(props) {
super(props);
this.state = {
noteArray : [
{
'date' : '',
'note' : ''
}
],
};
}
componentDidMount() { this.onLoad(); }
onLoad = async()=> {
storeVar.map(async(value, index) => {
try {
var d = new Date();
// const storedVal = [await AsyncStorage.getItem(storeVar[index].n)];
alert("Key is : " + JSON.stringify(storeVar[index-1].n, null, 4));
// this.state.noteArray.push({date :d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate(), note : storedVal[index]});
}
catch(error) { alert('Error' + error) }
});
}
只显示Hello,不显示文字Awesome。任何帮助将不胜感激。
答
不要使用index-1
,使用
JSON.stringify(storeVar[index].n, null, 4));
该指数将从0
开始,这样做的0
-1
将导致对-1
,并且它将无法访问件array
,当-1th索引变为1
,1 - 1
将为0
,那时它的打印Hello(0th
索引元素)。
现在问题就解决了! –