简体   繁体   English

我对对象数组使用了 map 方法,但是这个数组中的一个项目也是对象数组所以我需要获取它的属性

[英]I used map method for array of objects items but one item inside this array is also array of object So I need to take its properties

this is in the shared folder inside src in react app这是在反应应用程序中 src 内的共享文件夹中

export const DISHES =  [
        {
        id: 0,
        name:'sss',
        image: 'assets/images/ssss.png',
        category: 'dddd',
        comments: [
            {
            id: 0,
            rating: 5,
            comment: "dddd",
            author: "ddd",
            date: "3353"
            }
    ]

this is in App.js这是在 App.js 中

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      dishes: DISHES,
    };
  }
  
  render() {
    return (
      <div className="App">
        <Navbar dark color="primary">
          <div className="container">
            <NavbarBrand href="/">Ristorante Con Fusion</NavbarBrand>
          </div>
        </Navbar>
        <Menu dishes={this.state.dishes}/>
      </div>
    );
  }
}

and this render method from menuComponent.js和这个来自 menuComponent.js 的渲染方法

render() {
    const menu = this.props.dishes.map((dish) => {
      return (
        <div className="col-12 col-md-6 my-1">
          <Card key={dish.id} onClick={() => this.onDishSelect(dish)}>
            <CardImg width="100%" src={dish.image} alt={dish.name} />
            <CardImgOverlay>
              <CardTitle>{dish.name}</CardTitle>
            </CardImgOverlay>
      {/* <p>{dish.map(dish => <div>{dish.comments}</p> */}
          </Card>
        </div>
      );
    });
    return (
      <div className="container">
        <div className="row">{menu}</div>
        <div className="row">{this.renderDish(this.state.selectedDish)}</div>
      </div>
    );

  }

Now, I need to use the comments property that has an array of object and himself is one of the map items that I used before in the render Method现在,我需要使用具有对象数组的 comments 属性,而他自己是我之前在渲染方法中使用的地图项之一

To map the inner array you just need to access the properties correctly.要映射内部数组,您只需要正确访问属性。 comments property of the dish , and the comment property of each element of the comments array, this can be object destructured for brevity. dish comments属性,以及comments数组中每个元素的comment属性,为了简洁起见,可以进行对象解构。

dish.comments.map(({ comment }) => <div>{comment}</div>)

Code:代码:

render() {
    const menu = this.props.dishes.map((dish) => {
      return (
        <div className="col-12 col-md-6 my-1">
          <Card key={dish.id} onClick={() => this.onDishSelect(dish)}>
            <CardImg width="100%" src={dish.image} alt={dish.name} />
            <CardImgOverlay>
              <CardTitle>{dish.name}</CardTitle>
            </CardImgOverlay>
            <p>
              {dish.comments.map(({ comment }) => <div>{comment}</div>)}
            </p>
          </Card>
        </div>
      );
    });
    return (
      <div className="container">
        <div className="row">{menu}</div>
        <div className="row">{this.renderDish(this.state.selectedDish)}</div>
      </div>
    );
  }

暂无
暂无

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

相关问题 我如何 map 一个对象数组,里面也有一个对象数组? - How do I map an array of objects, that also have an array of objects inside it? 如何在 loDash 中使用 reduce 方法? 或者 Javascript 获取一组对象并创建一个对象 - How do I use the reduce method in loDash? Or Javascript Take a Array of Objects and Make one Object 如何获取数组中的对象? - How can i take the objects inside in the array? 如何在基于数组的对象数组中过滤 object 数组,并删除该 object 的属性? - How to filter an array of object inside an array of objects based on an array and also remove properties of that object? 我需要将我的对象数组转换为具有特定键的 object - I need to convert my array of objects into one object with specific keys 如何获取所有对象的属性并将它们插入到自己的对象数组属性中? - How do I take all of an object's properties and insert them into its own object array property? 将object里面的对象数组转换成属性数组 - Convert array of objects inside in object into array of properties 按地图内数组项内的对象对对象数组进行排序 - Sort array of objects by objects inside of array items inside map 如何将数组中的这些项目移动到数组中的对象中? - How would I move these items inside an array into objects within an array? 如果数组对象的 2 个属性相等,我如何删除数组中的对象? - How can i remove the object in the array if 2 properties of array objects are equal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM