I have an array and I want to convert the empty/undefined values to null. I have tried the following:
let arr = []
arr[1] = 2
arr[7] = 10
arr = arr.map(x => x || null)
console.log(arr)
But the output of the snippet is not as desired:
[ undefined, 2, undefined, undefined, undefined, undefined, undefined, 10 ]
How do I replace these undefined values with nulls? TIA
Note: Both vanilla JS and lodash answers are acceptable.