简体   繁体   English

如何在React JS中的对象内的数组上进行迭代

[英]How to iterate over an array that's inside an object in React JS

Imagine I have an array of objects like so: 想象一下,我有一个像这样的对象数组:

const chefs = [
  {
    key: 1,
    images: ['http://lorempixel.com/400/400/sports/', 'http://lorempixel.com/400/400/abstract/', 'http://lorempixel.com/400/400/animals/', 'http://lorempixel.com/400/400/city/', 'http://lorempixel.com/400/400/people/']
  }
]

And I'm wanting to assign each of the images inside of the images array to a React component called CarrouselItem using props: 而且我想使用props将images数组内的每个图像分配给一个名为CarrouselItem的React组件:

class CarrouselItem extends React.Component {
  render() {

    return (
      <div>
        <img src={this.props.imageUrl} />
      </div>
    )
  }
}

Which is then placed into a parent component called Carrousel , where I loop over the object and return the images inside, to be placed inside of the CarrouselItem component: 然后将其放置在称为Carrousel的父组件中,在该父组件上循环遍历对象并返回内部的图像,以将其放置在CarrouselItem组件内部:

class Carrousel extends React.Component {
  render() {
    return (
      <div>
        <h4>Slide image</h4>
        {chefs.map((chef, index) => {
          return <CarrouselItem imageUrl={chef.images[index]} />
        })}
      </div>
    )
  }
}

What I expect to happen 我期望发生什么

The map function should iterate over all of the objects (in this case, just one of them) which then creates X number of CarrouselItem components, each with an <img> that's taken from the images array. map函数应该遍历所有对象(在这种情况下,只是其中的一个),然后创建X个CarrouselItem组件,每个组件都带有从images数组中提取的<img>

What actually happens 实际发生了什么

Unfortunately I'm only getting the first item in the array (in this case, 'http://lorempixel.com/400/400/sports/' ). 不幸的是,我只得到数组中的第一项(在本例中为'http://lorempixel.com/400/400/sports/' )。

Could someone be so kind to explain where I'm going wrong here? 有人这么善良地解释我在哪里出问题了吗?

Link to CodePen. 链接到CodePen。

First you need to iterate over chefs array, and then individual images. 首先,您需要遍历Chefs数组,然后遍历单个图像。

 const chefs = [ { key: 1, images: ['http://lorempixel.com/400/400/sports/', 'http://lorempixel.com/400/400/abstract/', 'http://lorempixel.com/400/400/animals/', 'http://lorempixel.com/400/400/city/', 'http://lorempixel.com/400/400/people/'] } ] class Carrousel extends React.Component { render() { return ( <div> <h4>Slide image</h4> {chefs.map((chef, i)=>( <div key={i}> {chef.images.map((image,index)=>( <CarrouselItem imageUrl={image} key={index} /> ))} </div> ))} )) </div> ) } } class CarrouselItem extends React.Component { render() { return ( <div> <img src={this.props.imageUrl} /> </div> ) } } ReactDOM.render(<Carrousel />,document.getElementById("app")) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

The reason why its only giving you one item, is because you are mapping over a chef and not his images.. so you only iterate once. 之所以只给您一件商品,是因为您映射的是厨师,而不是他的图片。所以您只需要迭代一次。 Instead you just want to map over the images like so chefs.images.map 相反,您只想映射像chefs.images.map这样的图像

const chefs = [
  {
    key: 1,
    images: ['http://lorempixel.com/400/400/sports/', 'http://lorempixel.com/400/400/abstract/', 'http://lorempixel.com/400/400/animals/', 'http://lorempixel.com/400/400/city/', 'http://lorempixel.com/400/400/people/']
  }
]

class Carrousel extends React.Component {
  render() {
    const elems = [];
    chefs.forEach( (chef) => {
        chef && chef.images.forEach( (image, j) => {
            elems.push(<CarrouselItem imageUrl={image} key={i} />)
        })
    })
    return (
      <div>
        <h4>Slide image</h4>
        {elems}
      </div>
    )
  }
}

I would recommend you make a chef component that renders things like the chefs information, name, age, location, restaurant... etc.. and renders a carousel for each chef with their images. 我建议您制作一个厨师组件,以呈现诸如厨师信息,姓名,年龄,位置,餐厅等之类的信息,并为每个厨师及其图像提供轮播。

actually its picking chef object index which is one so you getting first instead use chefs.images.map 实际上,它的挑选厨师对象索引是一个,因此您可以首先使用chefs.images.map

{chefs[0].images.map((image, index) => {
          return <CarrouselItem imageUrl={image} />
        })}

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

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