javascript - 在javascript中循环对象
问题描述
我知道这是我应该知道的,但我很难弄清楚。
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内部的每个对象:
food.design.map(item => { /* do something here */ });
推荐阅读
- assembly - 为什么我在调试器中看不到地址布局随机化?
- angular - 如何从另一个角度组件激活一个组件中的 rxjs 订阅者?
- ios - 清除 Siri 快捷方式并不能完全奏效
- java - 如何使用列表的最终实例设计链表
- firebase - 如何在 3rd 方主机上托管 vuejs/firebase 应用程序
- rust - 为多个借用实现一个特征
类型导致实现冲突 - julia - Julia:可变结构项的多次调度
- javascript - 带有数据属性的 Angular 的“ng-repeat”函数的纯 javascript/jquery 解决方案?
- java - 将路径图像从java发送到python
- laravel - Laravel多个数据库队列不起作用