javascript - 如何显示列表对象的信息
问题描述
我有按类别属性分类的书籍,如何获取书籍的描述值以显示在屏幕上?
已经尝试过使用values()和keys()
{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
我需要返回对象书目的标题
解决方案
这段代码应该可以工作。使用Object.keys
and reduce
,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))
推荐阅读
- python - python Django中如何区分上传的PDF通过正则表达式提取数据
- flutter - 从 mongodb 反序列化几何多边形
- lisp - AutoLISP - 自动更改图层
- javascript - 将 Javascript 日期创建为 UTC
- visible - 有限的地面观察
- java - 如何在 java 中声明一个超过 64 位的数字?
- .net-core - 从 dotnetcore 应用程序调用 Microsoft 图形 API 会返回 NotFound
- windows - 基于公共列创建一个 .bat 文件,输出将是选择性列
- c++ - C++ 单独赋值还是创建一个新对象?
- javascript - React - 轮盘赌旋转器的算法