我有两个数组
a = [2,3,1,4]
b = [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
如何根据 a
对 b
进行排序?我想要的输出结果是
c = [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
我更倾向于使用Ramda或普通JS。
我有两个数组
a = [2,3,1,4]
b = [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
如何根据 a
对 b
进行排序?我想要的输出结果是
c = [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
我更倾向于使用Ramda或普通JS。
const {indexBy, prop, map, flip} = R
const a = [2,3,1,4]
const b = [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
const toIndexById = indexBy(prop('id'))
const findIndexIn = flip(prop)
const c = map(findIndexIn(toIndexById(b)), a)
console.log(c)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
你可以简单地使用Reduce。
var a = [2, 3, 1, 4];
var c = a.reduce((res,item)=>([...res,{'id':item}]),[])
console.log(c)
b
对象除了id
之外还包含其他内容吗? - georg