1 回答
TA贡献1843条经验 获得超7个赞
将数组映射为[name, value]pair数组,然后使用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>
使用 vanilla JS,您可以通过映射数组、使用解构来创建对(条目)的数组,然后使用以下方法转换为对象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)
添加回答
举报