I have an array
const arr1 = ["string1", "string2"]
const arr2 = []
And I want to get the first element of this array, or null if it is empty.
How can I do this using Ramda?
You can use R.propOr() to get a value from an object or array by key/index, and return a default if the result is undefined:
const firstOrNull = R.propOr(null, 0)
const arr1 = ["string1", "string2"]
const arr2 = []
console.log(firstOrNull(arr1))
console.log(firstOrNull(arr2))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>