javascript - Javascript - 将 javascript 对象的属性值转换为索引数组
问题描述
我有这个 javascript 对象:
{
"recommendations": [
{
"thing": "spiderman",
"weight": 1.6666666666666667,
"last_actioned_at": "2019-05-17T23:06:54+01:00",
"last_expires_at": "2020-06-06T01:00:00+01:00",
"people": [
"bob",
"alice"
]
},
{
"thing": "xmen",
"weight": 1.6666666666666667,
"last_actioned_at": "2019-05-17T23:06:54+01:00",
"last_expires_at": "2020-06-06T01:00:00+01:00",
"people": [
"alice",
"bob"
]
},
{
"thing": "barbie",
"weight": 1,
"last_actioned_at": "2019-05-17T23:06:54+01:00",
"last_expires_at": "2020-06-06T01:00:00+01:00",
"people": [
"alice"
]
},
{
"thing": "avengers",
"weight": 0.6666666666666667,
"last_actioned_at": "2019-05-17T23:06:54+01:00",
"last_expires_at": "2020-06-06T01:00:00+01:00",
"people": [
"bob"
]
}
],
"neighbourhood": {
"bob": 0.6666666666666667,
"alice": 1
},
"confidence": 0.002462038997842016
}
如何创建一个名为 indexe 数组results
,其值是所有thing
属性的值。
像这样:
['spiderman','xmen','barbie','avengers']
但如果我尝试:
let results = results_.map(a => a.thing);
我明白了
Unhandled rejection TypeError: expecting an array, a promise or a thenable
..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... …………
我的问题与从对象数组中提取属性值作为数组不同,因为这要求数组是输入变量,所以给出的答案对我不起作用。我现在已经得到了解决方案,所以请取消标记我的帖子所以我可以发布解决方案。
解决方案
推荐阅读
- docker - 无法使用卷
- pulumi - 检索 VPC 以创建安全组
- unity3d - 使用 RayCastAll 代替 RayCast
- python - 如何在 jupyter 事件小部件上输入和输出参数(单击按钮)
- reporting-services - 为什么我的参数显示错误无法评估 DataSet 的 FilterValues?
- mongodb - Grails MongoDB 插件:v:2 索引键模式中的值不能是 bool 类型
- docker - 将 Dockerfile 指定为管道作业容器
- python - Python tkinter 按钮在 -while 循环条件下不起作用
- python - 使用lmfit python将模型函数参数定义为自变量的函数
- google-cloud-platform - 将数据从变量插入 BQ 表时动态处理 Bigquery 表模式