Javascript recipes.map不是reactjs中的函数
我试图将数组中的元素作为reactjs中的组件输出,但得到一个错误“recipes.map不是函数” 下面是我的代码Javascript recipes.map不是reactjs中的函数,javascript,reactjs,Javascript,Reactjs,我试图将数组中的元素作为reactjs中的组件输出,但得到一个错误“recipes.map不是函数” 下面是我的代码 fetchRecipes() { var recipes = localStorage.getItem('recipes'); return recipes.map((i) => { return <ListItem key={i.recipeName} edit={this.props.edit} delete={this.
fetchRecipes() {
var recipes = localStorage.getItem('recipes');
return recipes.map((i) => {
return <ListItem key={i.recipeName} edit={this.props.edit} delete={this.props.delete} name={i.recipeName}/>
});
}
render() {
return (
<div>
{this.fetchRecipes()}
</div>
);
}
localStorage存储字符串,因此需要将字符串转换为数组
var recipes = JSON.parse(localStorage.getItem('recipes'));
当您存储它时,我希望您正在使用stringify
localStorage.setItem('recipes', JSON.stringify(recipes));
使用ES2017,使用
Object.values()
日志:
与ES2015相同
Object.keys(json).map((i) => console.log((json[i].recipeName)));
因为它是一个字符串…您确定localStorage.getItem为您提供了一个数组吗?
var json = [{"recipeName":"Tomatoes","recipeIngredients":"somtheng, something"}, {"recipeName":"test","recipeIngredients":"somtheng, something"}];
var objects = Object.values(json);
objects.map((value) => {
console.log(value.recipeName);
})
Tomatoes
test
Object.keys(json).map((i) => console.log((json[i].recipeName)));