wordpress multiple image selection gives one empty “id”

I am trying to save all IDs from selected images as a string. It works almost perfect, but it is one problem. Every time a user select a photo, the code makes one empty value. In the console if I watch the IDs it returns this: [“”, “66”, “125”, “97”, “58”, “59”]

Why does it return one “” ? any ideas?

    imageUploader.on( 'close', function() {
    //var attachment = imageUploader.state().get('selection').first().toJSON();
    var attachment = imageUploader.state().get('selection').toJSON();
    var ids = attachment.map( function (attachment) {
        return attachment.id;
        console.log(ids);
    });
    jQuery.each(attachment, function(){
     jQuery('#image-tag').prepend('<img src="'+attachment.id+'" />')
  });
    hidden.setAttribute( 'value', ids.join(',') );
  });

Related posts