Map the the array into an array of [name, value] pairs, and then convert it to an object using R.fromPairs:

const { pipe, fromPairs, map, props } = R

const fn = pipe(map(props(['name', 'value'])), fromPairs)

const arr = [{ name: "firstName", value: "John" }, {name: "lastName", value: "Doe" }]

const result = fn(arr)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

With vanilla JS you can create the array of pairs (entries) by mapping the array, using destructuring, and then converting to object using Object.fromEntries():

const fn = arr => Object.fromEntries(arr.map(({ name, value }) => [name, value]))

const arr = [{ name: "firstName", value: "John" }, {name: "lastName", value: "Doe" }]

const result = fn(arr)

console.log(result)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top