如何将值转换为对象数组

Luk*_*101 2 javascript underscore.js lodash

假设我有这些数据:

var id = 81;
var categories = [1, 2, 3, 4, 5];
Run Code Online (Sandbox Code Playgroud)

我如何将其转换为:

[{id: 81, category: 1}, {id: 81, category: 2}, {id: 81, category: 3}, {id: 81, category: 4}, {id: 81, category: 5}]
Run Code Online (Sandbox Code Playgroud)

有没有一种优雅的方法来使用下划线或lodash?

Ром*_*еев 9

这里不需要图书馆.

const result = categories.map(x => ({ id, category: x }))
Run Code Online (Sandbox Code Playgroud)