admin管理员组文章数量:1332403
I am trying to loop through an array of objects in an external json file using the map function. The loop succeeds but I am not sure how to access the object properties. See below.
I am using object.keys(obj).map() but cannot get access to the individual properties. Map keeps outputting the array index numbers.
This is my data I want to iterate through.
[
{
"id" : "12345",
"customer" : "BMW",
},
{
"id" : "45678",
"customer" : "Ford",
},
{
"id" : "78901",
"customer" : "Google",
}
]
I have a state hook that the data will be saved to
const [accountData, setAccountData] = useState('');
The function below gets the data from the external json file and sets the state with it.
axios.get('./data/account-info.json')
.then((response) => {
//set state
setAccountData(response.data);
})
.catch((error) => {
console.log(error);
});
I iterate through the state object with a map function
Object.keys(accountData).map((id,customer) => {
return(
<div>
<p>ID: {id}</p>
<p>Name: {customer}</p>
</div>
)
})
The output keeps printing out the index numbers instead of the appropriate values
//outputted elements
<div>
<p>ID: 0</p>
<p>Name: 0</p>
</div>
<div>
<p>ID: 1</p>
<p>Name: 1</p>
</div>
<div>
<p>ID: 2</p>
<p>Name: 2</p>
</div>
Can you please tell me what I am doing wrong here? I know it has to be something simple.
I am trying to loop through an array of objects in an external json file using the map function. The loop succeeds but I am not sure how to access the object properties. See below.
I am using object.keys(obj).map() but cannot get access to the individual properties. Map keeps outputting the array index numbers.
This is my data I want to iterate through.
[
{
"id" : "12345",
"customer" : "BMW",
},
{
"id" : "45678",
"customer" : "Ford",
},
{
"id" : "78901",
"customer" : "Google",
}
]
I have a state hook that the data will be saved to
const [accountData, setAccountData] = useState('');
The function below gets the data from the external json file and sets the state with it.
axios.get('./data/account-info.json')
.then((response) => {
//set state
setAccountData(response.data);
})
.catch((error) => {
console.log(error);
});
I iterate through the state object with a map function
Object.keys(accountData).map((id,customer) => {
return(
<div>
<p>ID: {id}</p>
<p>Name: {customer}</p>
</div>
)
})
The output keeps printing out the index numbers instead of the appropriate values
//outputted elements
<div>
<p>ID: 0</p>
<p>Name: 0</p>
</div>
<div>
<p>ID: 1</p>
<p>Name: 1</p>
</div>
<div>
<p>ID: 2</p>
<p>Name: 2</p>
</div>
Can you please tell me what I am doing wrong here? I know it has to be something simple.
Share Improve this question asked Aug 6, 2019 at 20:02 David SandersDavid Sanders 1311 gold badge3 silver badges16 bronze badges5 Answers
Reset to default 3You can iterate accountData
directly, as it's an array.
Each item will be an object, so you need to destructure as you see below.
(you don't need to but that's was the intention I perceived from your code)
And also you need to add a key to each element, to notify React how to keep track of elements.
//
本文标签:
javascriptHow to iterate through an array of objects from json in ReactjsStack Overflow
版权声明:本文标题:javascript - How to iterate through an array of objects from json in React.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1742278383a2445630.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论