There is a code that takes all files from input of multiple files, gets base64 for each of them, packs to array and jsonifies it. How to simplify and improve quality of code?text = $('#input-' + id).val();var files = document.querySelector('#files' + id + ' > input[type=file]').files;if (text != '' || files.length > 0) {    var files_obj = [];    var reader = new FileReader();    reader.onload = function(readerEvt) {        var binaryString = readerEvt.target.result;        files_obj.push(btoa(binaryString));    };    for (var i = 0; i < files.length; i++) {        reader.readAsBinaryString(files[i]);    }    while (files_obj.length < files.length) {}    $('input[type=file]').val();    files_json = JSON.stringify(files_obj);    json = '{action: message, from: ' + userId + ', text: ' + text + ', room: ' + id + ', files: ' + files_json + '}';    socket.send(json);    $('#input-' + id).val();}
Sending file[type=file] multiple content as list of base64's
javascript