繁体   English   中英

读取对象数组中的对象数组

[英]read an array of objects in array of object

我有一个问题是在一个对象数组中映射一个对象数组...

这是我的阵列

const demoCompletedData = [
  {
    id: 1,
    marketId: "1111-1111-1111-1111",
    title: "Autonomous car",
    picture: "https://th?id=OIP.fdvfdvfdvfdvfd&pid=Api",
    language: "English",
    flag: "🇬🇧",
    completed: true,
    date: "22/01/2019 10:30",
    rate: 9.1,
    categories: {
      de: 1,
      sp: 2,
      uk: 0,
      fr: 1,
      us: 4,
    },
  },
module.exports = demoCompletedData;

我的代码在前面阅读:fetchDemo

  fetchDemo() {
    this.props.demoFetchRequest();
    const { marketId } = this.props.match.params;
    axios.get(`/api/v1/passport-authenticate/market/${marketId}`)
      .then((res) => {
        return res.data;
      })
      .then(demo => this.props.demoFetchSuccess(demo))
      .catch(error => this.props.demoFetchError(error));
  }

我的归来和归来,我的归来和归来

const { demo } = this.props;

以及我的渲染和渲染以及渲染和渲染

<p>
            Categories :
            {
              Object.values(`${demo.categories}`).map((category) => {
                return (
                  <ul>
                    <li>
                      {category.toString()}
                    </li>
                  </ul>
                );
              })}
          </p>

如何解决这个问题?

编辑:

谢谢你的帮助。 我想阅读'类别'并将其映射为显示'de','fr','us','uk'的价值......但我完全迷失了!

{
              Object.keys(demo).filter((x) => { return x === 'categories'; }).map((category) => {
                return (
                  <ul>
                    <li>
                      {category.de}
                    </li>
                  </ul>
                );
              })}

类似的东西:

{category.de > 0 ? `de : ${category.de}` : ''}
{category.us > 0 ? `us : ${category.us}` : '' }

您的类别是对象,您正在尝试将其转换为字符串。 试试下面的代码。

如果你想要钥匙那么

<li>
    {Object.keys(category)[0]}
</li>

如果你想要价值那么

<li>
    {Object.values(category)[0]}
</li>

暂无
暂无

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

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