【问题标题】:How to iterate through the result of a cloudinary query如何遍历云查询的结果
【发布时间】:2021-10-22 09:52:18
【问题描述】:

我将一个视频上传到 cloudinary,该视频通过“谷歌视频审核”进行了审核。如果视频审核状态获得批准,我想保存视频的 public_Id 和 url。我已经能够成功地为视频查询 cloudinary,但我不知道如何遍历查询结果。我需要使用 NodeJS 从结果中迭代并仅获取 public_Id 和 url。 我还没有尝试过任何东西,因为我是 NodeJS 的新手。

查询结果:

{
  resources: [
    {
      asset_id: '54598f6f952b934d45b9b8dfa3b9298f',
      public_id: 'm7zp9okftllenzmigyng',
      format: 'mp4',
      version: 1629500308,
      resource_type: 'video',
      type: 'upload',
      created_at: '2021-08-20T22:58:28Z',
      bytes: 1004360,
      width: 640,
      height: 352,
      url: 'http://res.cloudinary.com/dt3ic2vk7/video/upload/v1629500308/m7zp9okftllenzmigyng.mp4',
      secure_url: 'https://res.cloudinary.com/dt3ic2vk7/video/upload/v1629500308/m7zp9okftllenzmigyng.mp4'
    }
  ],
  rate_limit_allowed: 500,
  rate_limit_reset_at: 2021-08-21T00:00:00.000Z,
  rate_limit_remaining: 490
}

【问题讨论】:

    标签: javascript node.js cloudinary


    【解决方案1】:

    我们可以使用Array.prototype.forEach() function 遍历resources 对象中的所有对象(视频),并使用Array.prototype.push() function 将值添加到数组中(作为示例)。

    // initialize a new array called "results"
    let results = [];
    
    // for each object (initialized as "videoObject") in the "resources" object
    result.resources.forEach(videoObject => {
        // add the "public_id" and "url" keys into the "results" array.
        results.push(videoObject.public_id, videoObject.url)
    });
    

    【讨论】:

    • 这很有帮助,虽然我并不真的需要初始化一个数组。我只是将 public_id 和 URL 保存在不同的变量中,然后根据需要使用它们。谢谢
    • 对不起大声笑,就像我初始化一个数组的例子一样。没问题!
    • 请检查我的答案旁边的绿色复选标记,以表明此问题已回答。
    猜你喜欢
    • 2015-12-16
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2016-07-08
    相关资源
    最近更新 更多