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

时间:2019-04-29 21:05:58

标签: javascript node.js reactjs

我在将对象数组映射到对象数组时遇到问题...

这是我后面的阵列

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}` : '' }

1 个答案:

答案 0 :(得分:0)

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

如果要输入密钥

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

如果您想增值

<li>
    {Object.values(category)[0]}
</li>
相关问题