如何显示列表对象的信息

时间:2019-06-20 14:28:40

标签: javascript arrays reactjs object

我有按类别属性分类的书籍,如何获得书籍的描述值以显示在屏幕上?

已经尝试使用值()和键()

{1: Array(2), 2: Array(1), 4: Array(1), 9: Array(1)}

1: Array(2)
0: {id: 1, description: "teste", category: 1}
1: {id: 73, description: "basica tb", category: 1}
length: 2
__proto__: Array(0)
2: Array(1)
0: {id: 3, description: "Teoria das ideias", category: 2}
length: 1
__proto__: Array(0)
4: Array(1)
0: {id: 5, description: "Mr with research computer.", category: 4}
length: 1
__proto__: Array(0)
9: Array(1)
0: {id: 10, description: "Vote drug thus no.", category: 9}
length: 1
__proto__: Array(0)
__proto__: Object

我需要返回对象书目的标题

2 个答案:

答案 0 :(得分:0)

此代码应该起作用。使用Object.keysreduce对每个值进行map并返回描述:

const data = {
    1: [
        {id: 1, description: "teste", category: 1},
        {id: 73, description: "basica tb", category: 1}
    ],
    2: [
        {id: 3, description: "Teoria das ideias", category: 2}
    ],
    4: [
        {id: 5, description: "Mr with research computer.", category: 4}
    ],
    9: [
        {id: 10, description: "Vote drug thus no.", category: 9}
    ]
}

const getDescription = data => Object.keys(data).reduce((a, key) => ({...a, [key]: data[key].map(o => o.description)}), {})

console.log(getDescription(data))

答案 1 :(得分:0)

您好,古斯塔沃(Gustavo),我不太确定您想要什么,但我认为您想呈现对象列表。有很多方法可以做到,但是我将通过使用javascript高阶函数map向您展示最简单的方法。此高阶函数让您循环遍历数组。

简单实例:

import React from 'react';

const List = () => {
      const data = [{
      name: 'Joe',
      age: '16'
      }]
      return (
      {data.map(data => <li key={data.age}>{data.name}</li>)}
)
}