假设我有这个数据:
var id = 81;
var categories = [1, 2, 3, 4, 5];
我如何将其转换为:
[{id: 81, category: 1}, {id: 81, category: 2}, {id: 81, category: 3}, {id: 81, category: 4}, {id: 81, category: 5}]
是否有使用下划线或 lodash 的优雅方式来做到这一点?
最佳答案
这里不需要库。
const result = categories.map(x => ({ id, category: x }))
关于javascript - 如何将值转换为对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829606/