简体   繁体   English

React Map函数:如何一次输出所有嵌套数组的内容?

[英]React map function: how to output content of all nested arrays at once?

My data is an object with key value pairs and then some arrays ("skill_1" and "skill_2") with strings: 我的数据是一个具有键值对的对象,然后是带有字符串的一些数组(“ skill_1”和“ skill_2”):

const HeroDescriptions = [
  {
    id: "ana",
    name: "Ana",
    role: "Support",
      skill_1: [
        "Biotic Rifle",
        "Damage: 70",
        "Healing: 75"
      ],
      skill_2: [
        "Biotic Grenade",
        "Damage: 60",
        "Healing: 100"
      ]
    }
  ]

I output this to JSX like that: 我这样输出到JSX:

const content = description.map(item => (
    <div key={item.id}>
      <h1>{item.name}</h1>
      <h2>Role: {item.role}</h2>
      <hr />
      <ul>
        {description[0].skill_1.map((skill, index) => (
          <li key={index}>{skill}</li>
        ))}
      </ul>
    </div>
  ));

  return (
    <div>
      <ul>{content}</ul>
    </div>
  );
};

I would like to modify this snippet: 我想修改以下代码段:

{description[0].skill_1.map((skill, index) => (
   <li key={index}>{skill}</li> 

How could I loop through all nested skill arrays, so that my code would be reusable and would work, if there were eg 4 arrays with skills? 我如何遍历所有嵌套的技能数组,以使我的代码可以重复使用并可以工作,如果有四个带有技能的数组?

Hello fellow Overwatch player :) 您好守望先锋玩家:)

If you want to loop over keys of an object, you can use Object.keys method which is described here 如果要遍历对象的键,则可以使用此处描述的Object.keys方法

Your code block would become: 您的代码块将变为:

Object.keys(item)
  .filter(key => ['id', 'name', 'role'].indexOf === -1))  // filter out unrelated keys
  .map(skillKey => item[skillKey])
  .map(skillArr => <li key={skillArr[0]}>{skillArr.join(', ')}</li>)

PS: You data object is not well formed. PS:您的数据对象格式不正确。 It would be better to have another field called 'skills' and loop over them 最好有一个名为“技能”的领域并遍历它们

Using your existing data structure, you can use spread syntax to capture the skills together, and then using the getSkills method, reduce over each object's values to combine the arrays, and then map over that set to produce your final list of skills. 使用现有的数据结构,可以使用spread语法将技能一起捕获,然后使用getSkills方法, reduce每个对象的值以组合数组,然后map到该集合以生成最终的技能列表。

 class App extends React.Component { getSkills(skills) { return Object.values(skills).reduce((acc, skillset) => { return [...acc, ...skillset]; }, []).map(skill => <li>{skill}</li>); } render() { const { descriptions } = this.props; return ( descriptions.map(params => { const { id, name, role, ...skills } = params; return ( <div key={id}> <h1>{name}</h1> <h2>Role: {role}</h2> <hr /> <ul> {this.getSkills(skills)} </ul> </div> ); }) ); } } const descriptions = [{"id":"ana","name":"Ana","role":"Support","skill_1":["Biotic Rifle","Damage: 70","Healing: 75"],"skill_2":["Biotic Grenade","Damage: 60","Healing: 100"]}]; ReactDOM.render( <App descriptions={descriptions} />, document.querySelector('#app') ); 
 .category { margin: 1em; } h1 { font-size: 1em; font-weight: 600; } h2 { font-size: 0.9em; font-weight: 600; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div> 

You may extract skill_x properties into array: 您可以将skill_x属性提取到数组中:

 const HeroDescriptions = [{ id: "ana", name: "Ana", role: "Support", skill_1: [ "Biotic Rifle", "Damage: 70", "Healing: 75" ], skill_2: [ "Biotic Grenade", "Damage: 60", "Healing: 100" ] } ]; ({id, name, role, ...skills} = HeroDescriptions[0]); console.log(Object.values(skills)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

And I agree with above comments, reformatting those properties into something, like below, would make more sense: 我同意上面的评论,将这些属性重新格式化为如下所示的内容会更有意义:

{
    skill: 'Biotic Rifle',
    damage: 70,
    Healing: 75
}

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

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