Search code examples
javascriptnode.jscloudinary

How to iterate through the result of a cloudinary query


I uploaded a video to cloudinary that's moderated with the "google video moderation". I want to save the public_Id and the url of the video if the video moderation status is approved. I've been able to successfully query cloudinary for the video, but I don't know how to iterate through the result of the query. I need to iterate and get only the public_Id and the url from the result with NodeJS. I've not tried anything yet as I am new to NodeJS.

The result of the query:

{
  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
}

Solution

  • We can use the Array.prototype.forEach() function to iterate through all the objects (videos) in the resources object and use Array.prototype.push() function to add the values into an array (as an example).

    // 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)
    });