How to consecutively pass values in an array as the argument of a function

I want to apply the values in an array as the first argument of a function for each value in the array.
E.g. So for arg1 in foo:

foo(arg1, arg2, arg3)

var myArray = [ 'image1.jpg', 'image2.jpg', 'image3.jpg' ]

I want to apply each image as the first argument for every image in the array:
foo(image1.jpg, arg2, arg3)
foo(image2.jpg, arg2, arg3)
foo(image3.jpg, arg2, arg3)

I was thinking the .apply method might be a way to do this but not been able to get this to work. Is this on the right track or would a for loop be needed?
For example:

myFunction.apply(this, myArray)
myFunction = foo(this, arg2, arg3)

The context is - I’m a js novice using node.js and want to pass an array of files to cloudinary’s upload function so I can upload a batch of images (rather than a single image which works fine).

The Cloudinary upload method performs an authenticated upload API call over HTTPS while sending the image file:
cloudinary.v2.uploader.upload(file, options, callback);

From the forum it suggests I can

“migrate existing images to Cloudinary, write a short script that traverses your images and upload them one-by-one using Cloudinary’s upload API.”

I’m trying to achieve this by using fs-readdir-recursive to obtain an array of all the files in a specified folder.

Thanks!

apply doesn’t help you here because you want to use each image as the first parameter.

since you’re working in Node.js, have a look at the async library. this is written for such cases.

Thanks for the reply Dormilich.
I will take a look into the async library and see what I can glean from it.
thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.