【问题标题】:Return contents of the first item at index返回索引处第一项的内容
【发布时间】:2019-08-13 13:27:59
【问题描述】:

我正在 Zapier 中构建一个自定义集成,并遇到了一个问题,即我(对 Mavenlink)进行的 API 调用正在返回一个带有任意键的对象(在下面的示例中为 235896815)。但是,Zapier 在返回结果时只能使用数组(而不是对象)。我需要一种方法来获取返回对象中的数组而不知道键是什么。我有一个潜在的想法是始终返回索引处第一个对象的结果。这会是解决这个问题的最好方法吗?如果是这样,我将如何去做?也欢迎其他想法。

示例输出:

{
  "235896915": {
    "can_edit": true,
    "subject_type": "story",
    "account_id": 4150797,
    "subject_id": 390078195,
    "updated_at": "2019-03-21T14:26:16-07:00",
    "value": [
      1406325
    ],
    "display_value": "Nicole Patel",
    "setter_id": "10149395",
    "custom_field_id": "181017",
    "created_at": "2019-03-21T14:26:16-07:00",
    "custom_field_name": "Active Assignee",
    "type": "single",
    "id": "235896915"
  }
}

这是我正在打的电话:

const options = {
  url: `https://api.mavenlink.com/api/v1/custom_field_values.json?subject_type=story&with_subject_id=${bundle.inputData.subject_id}`,
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${bundle.authData.access_token}`,
    'Accept': 'application/json'
  },
  params: {
    'subject_id': bundle.inputData.with_subject_id,
    'id': bundle.inputData.id,
    'display_value': 'Active Assignee'
  }
}

return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = z.JSON.parse(response.content);

    // You can do any parsing you need for results here before returning them

    return results.custom_field_values; //return object in an array
  });
```

【问题讨论】:

  • 只有on key吗?在这种情况下,235896915。如果有多个,您要全部退回吗?

标签: javascript zapier


【解决方案1】:

您可以使用 Object.values 将对象转换为数组。

var arr = Object.values(results);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 2019-11-24
    • 1970-01-01
    • 2015-12-22
    • 2014-02-18
    相关资源
    最近更新 更多