如何在React组件内循环遍历数组中对象的属性?

3
这里是我的父组件的渲染函数:
  render() {
    const users = [
      'tom': {
        phone: '123',
        email: 'hotmail'
      },
      'rob': {
        phone: '321',
        email: 'yahoo'
      },
      'bob': {
        phone: '333',
        email: 'gmail'
      },
    ];

    const list = users.map((user) =>
      (<User
        name={user}
        phone={users.phone}
        email={users.email}
      />),
    );

    return <ul>{list}</ul>;
  }

输出结果如下:

在这里输入图片描述

以下是Child组件的渲染函数:
  render() {
    const {
      name,
      phone,
      email,
    } = this.props;

    const info = [name, phone, email];

    const item = info.map((index) => (
      <li key={index}>
        { index }
      </li>
    ));

    return item;
  }

我该如何让电话号码和电子邮件显示出来呢?不确定自己哪里做错了。谢谢。


你的 users 变量被赋值为一个看起来更像对象而不是数组的数组... - Code-Apprentice
5个回答

5

起初,这不是有效的JavaScript代码:

const users = [
  'tom': {
    phone: '123',
    email: 'hotmail'
  },
  // ...
];

你需要定义一个数组或一个对象。假设你的users是一个对象字面量:

const users = {
  'tom': {
    phone: '123',
    email: 'hotmail'
  },
  // ...
};

然后,您可以遍历对象的entries

const list = Object.entries(users).map(([name, info]) =>
  (<User
    name={name}
    phone={info.phone}
    email={info.email}
  />)
);

然而,不是所有浏览器都支持Object.entries()函数,请在您的环境中检查其是否可用。


1

首先,users不是一个有效的数组。如果你想在主范围内使用键值对,则应该使用Object({})。

 render() {
        const users = {
          'tom': {
            phone: '123',
            email: 'hotmail'
          },
          'rob': {
            phone: '321',
            email: 'yahoo'
          },
          'bob': {
            phone: '333',
            email: 'gmail'
          },
        };

        const list = Object.keys(users).map((user) =>
          (<User
            name={user}
            phone={users[user].phone}
            email={users[user].email}
          />),
        );

        return <ul>{list}</ul>;
      }

0
一个解决方案是将你的“list”更改为实际的列表:
const users = [
  {
    name: 'tom',
    phone: '123',
    email: 'hotmail'
  },
  {
    name: 'rob,
    phone: '321',
    email: 'yahoo'
  },
  {
    name: 'bob',
    phone: '333',
    email: 'gmail'
  },
];

现在使用user.name代替user

或者,创建一个由每个用户名称为键的对象:

const users = {
  'tom': {
    phone: '123',
    email: 'hotmail'
  },
  'rob': {
    phone: '321',
    email: 'yahoo'
  },
  'bob': {
    phone: '333',
    email: 'gmail'
  },
};

然后映射键:

const list = Object.keys(users).map((user) =>
  (<User
    name={user}
    phone={users[user].phone}
    email={users[user].email}
  />),
);

0

你的const users应该被修改为通过它们进行映射。

 const users = [
            {
            name: 'tom',
            phone: '123',
            email: 'hotmail',
          },
          {
            name:'rob',
            phone: '321',
            email: 'yahoo'
          },
          {
            name: 'bob',
            phone: '333',
            email: 'gmail'
          },
    ];
    const list = users.map((usr =>
      (<User
        name={usr.name}
        phone={usr.phone}
        email={usr.email}
      />),
    );

    return <ul>{list}</ul>;
  }

0

在父组件的map函数中,你需要引用“user”而不是“users”。 “user”变量的目的是表示数组中每个元素的当前实例。因此,map函数应该如下所示:

const list = users.map((user) =>
  (<User
    name={user}
    phone={user.phone}
    email={user.email}
  />),
);

取而代之。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接