【发布时间】:2020-06-07 04:04:38
【问题描述】:
I am making a call to firebase database and parsing through the JSON objects and storing them in an array locally. Then I am copying the array created to a state array object and later printing the values of the state array object inside the render() function.
class ABC extends Component
{
state = {
objectArray:[
{
key1:'',
key2:'',
key3:'',
key4:''
}
]
};
componentDidMount() {
var objectArray = [];
var object = {
key1:'',
key2:'',
key3:'',
key4:''
};
// parsing through the firebase object and storing it in the object which is happening successfully
objectArray.push(object);
//when I print the array it gives me the result: [object Object],[object Object] which means two objects are getting stored here.
this.setState({objectArray: objectArray });
}
render()
{
this.state.objectArray.map(function(item){
console.log("The items is "+item.key1); // No data getting printed here.
console.log("The items is "+item.key2);
console.log("The items is "+item.key3);
console.log("The items is "+item.key4);
});
}
我能够从 firebase 对象数组中获取结果并解析它们并将它们存储在 objectArray 本地对象中,但无法将它们存储在状态数组对象中。这里出了什么问题?
【问题讨论】:
标签: javascript arrays json react-native