Javascript | React Native 地图功能无法正常工作
Javascript | React Native map function not working properly
所以,我在使用 React Native 时发现了一个非常愚蠢的问题,我似乎无法理解。我已经声明了一个具有一些硬编码值的对象数组(此处为 storeVar)。当我尝试使用 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
的第 1 个元素,当索引变为 1
并且 1 - 1
将是 0
时它打印 Hello (0th
索引元素).
所以,我在使用 React Native 时发现了一个非常愚蠢的问题,我似乎无法理解。我已经声明了一个具有一些硬编码值的对象数组(此处为 storeVar)。当我尝试使用 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
的第 1 个元素,当索引变为 1
并且 1 - 1
将是 0
时它打印 Hello (0th
索引元素).