简体   繁体   English

在reactjs中循环数组数组

[英]Loop on array of arrays in reactjs

MAP es6 doesn't work with array of arrays in react, Data: MAP es6 不适用于反应中的数组数组,数据:

{
"skills": [{
    "field": {
        "id": 1,
        "name": {
            "en": "Web Developer"
        }
    },
    "skills": [{
        "id": 2,
        "name": {
            "en": "Blog"
        }
    }]
}, {
    "field": {
        "id": 3,
        "name": {
            "en": "UI/ UX Designer"
        }
    },

    "skills": [ {
        "id": 4,
        "name": {
            "en": "Web Interface"
        }
    }]
}]

} }

so in my render of jsx I do:所以在我的 jsx 渲染中,我这样做:

{item.skills.map(item => 
    <p>{item.skills.name}</p>
)} 

Reason is this.skills inside map is again an array , and name is an object , first you need to change the JSON structure, and to access the name you need to use name.en , check this:原因是this.skills里面的this.skills又是一个array ,而 name 是一个object ,首先你需要改变JSON结构,并访问你需要使用name.en的名称,检查这个:

 let data = { "skills": [{ "field": { "id": 1, "name": { "en": "Web Developer" } }, "skills": { "id": 2, "name": { "en": "Blog" } } },{ "field": { "id": 3, "name": { "en": "UI/ UX Designer" } }, "skills": { "id": 4, "name": { "en": "Web Interface" } } }] } data.skills.map((item)=>{ console.log(item.skills.name.en); })

If you defined skills inside the skills as an array and it will contain only one item, then you need to access the name like this:如果你将skillsskills定义为一个array并且它只包含一个项目,那么你需要像这样访问这个名称:

item.skills[0].name.en

Otherwise you need to run a map again to access the name.否则,您需要再次运行map以访问该名称。 like this:像这样:

data.skills.map((item)=>{
     item.skills.map((el)=>{
         console.log(el.name.en);
     });
})

Your skills[n].skills is array.你的skills[n].skills是数组。 And you need change the structure or execute .map again into your first map method.并且您需要更改结构或再次执行.map到您的第一个map方法中。 Like this:像这样:

{item.skills.map(item => 
    item.skills.map(skill => 
    <p>{skill.name[en]}</p>
    )
)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM