首页 > 解决方案 > 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

..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... …………

我的问题与从对象数组中提取属性值作为数组不同,因为这要求数组是输入变量,所以给出的答案对我不起作用。我现在已经得到了解决方案,所以请取消标记我的帖子所以我可以发布解决方案。

标签: javascriptarraysnode.js

解决方案


推荐阅读