Open this example in the interactive library
Source code
const words = "paper ink paper type".split(" ");
const index = new Map();
for (const [i, w] of words.entries())
index.set(w, [...(index.get(w) ?? []), i]);
console.log(Object.fromEntries(index));
Sample output
{ paper: [ 0, 2 ], ink: [ 1 ], type: [ 3 ] }