I have an array: var foo = [];
and I want to push files in it: foo.push(input.files[0])
But if I want to remove an item with a specific key with the delete foo[i]
, the array length won't reduce and the deleted item will be replaced with an empty / undefined
tag. Is there any way to completely remove items from an array?
var foo = []; var input = document.getElementById('input');
document.getElementById('add').addEventListener('click', () => {
foo.push(input.files[0]);
});
document.getElementById('delete').addEventListener('click', () => {
delete foo[0];
});
document.getElementById('check').addEventListener('click', () => {
console.log(foo);
console.log(foo.length);
});
<button id="add">ADD</button>
<button id="delete">DELETE</button>
<button id="check">CHECK</button>
<br><br>
<input type="file" id="input" placeholder="UPLOAD IMAGE">