我一直在尝试返回我的 JSON 对象中第一个数组的第一个对象,但我遇到了一些麻烦。
我想在标签中返回第一个“维生素”的名称,即“维生素 B2”。
这是我的 JSON 对象
{
"nutrients": {
"vitamins": [{
"name": "Vitamin B2",
"current": "1.7"
},
{
"name": "Vitamin B6",
"current": "10.9"
}],
"minerals": [{
"name": "Zinc",
"current": "1.7"
}]
}
}
这是我试图映射它的代码
import nutrients from '../../vitamins.json';
renderData() {
return Object.keys(nutrients).map((nutrient, index) => {
console.log('it works', nutrient[0].name, index);
return (
<option value="" key={index}>{nutrient[0].name}</option>
)
}
)}
最佳答案
var a ={
"nutrients": {
"vitamins": [{
"name": "Vitamin B2",
"current": "1.7"
},
{
"name": "Vitamin B6",
"current": "10.9"
}],
"minerals": [{
"name": "Zinc",
"current": "1.7"
}]
}
}
console.log(a.nutrients['vitamins'][0].name)
关于javascript - 在 React 中迭代 JSON 对象中的多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459944/