所以,我正在使用 React Native,发现了一个非常愚蠢的问题,我似乎无法理解。我已经声明了一个具有一些硬编码值的对象数组(此处为 storeVar(。当我尝试使用 javascript map 函数循环遍历它时,我只在第一个索引处获得值。代码如下 -
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) }
});
}
仅显示">您好",而不显示文本"真棒"。任何帮助将不胜感激。
不要使用
index-1
,使用
JSON.stringify(storeVar[index].n, null, 4));
该索引将从 0
开始,因此-1
0
将导致-1
,并且当索引变为1
并且1 - 1
将0
此时打印 Hello(0th
索引元素(时,它将无法访问array
的第 -1 个元素。