返回索引处第一项的内容

问题描述 投票:1回答:1

我正在Zapier中构建一个自定义集成,并遇到了一个问题,即我正在进行的API调用(到Mavenlink)返回一个具有任意键的对象(下例中为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
  });
```
javascript zapier
1个回答
0
投票

您可以将对象转换为具有Object.values的数组。

var arr = Object.values(results);
© www.soinside.com 2019 - 2024. All rights reserved.