【发布时间】:2019-06-19 15:32:25
【问题描述】:
我知道这是我应该知道的,但我无法弄清楚。
const food = {
"design": [
{
"forTemperature": {
"high": "100",
"low": "70"
},
"productURL": "",
"imageURL": ""
},
{
"forTemperature": {
"high": "80",
"low": "65"
},
"productURL": "",
"imageURL": ""
},
{
"forTemperature": {
"high": "75",
"low": "65"
},
"productURL": "",
"imageURL": ""
},
{
"forTemperature": {
"high": "67",
"low": "60"
},
"productURL": "",
"imageURL": ""
}
]
}
我拥有的基本 json 文件,数组中的对象,但执行以下操作:
food.design.map((item, i) => i
对我不起作用。
我需要采取什么方法?
import food from "./apiDesign.json";
const New = props => {
const [food, setFood] = useState([]);
useEffect(() => {
const fetchAPI = async e => {
const designer = await food;
setFood(designer);
};
fetchAPI();
}, [])
const stuff = () => {
return loop // I was trying to loop it in here
};
return (
{stuff}
)
}
export default New;
【问题讨论】:
-
你的预期输出是什么?
-
food.design.map((item, i) => i); // 返回 [0,1,2,3]。你能解释一下你需要哪些值吗?你还想 map() 还是循环?
-
@assoron @AswinKumar 我收到了
Cannot read property 'map' of undefined -
@hellomello 您在什么环境中运行代码?因为我很确定在通常的在线 REPL 上运行良好。
-
@assoron 我正在使用 reactjs 在codesandbox.io 中运行它。这就是你要问的吗?对不起!另外,感谢您的帮助!
标签: javascript arrays json javascript-objects