Open this example in the interactive library
Source code
import { Readable } from "node:stream";
import { createInterface } from "node:readline";
const input = Readable.from([
'{"title":"Tide","tags":["sea","blue"]}\n',
'{"title":"Dune","tags":["sand","gold"]}\n',
'{"title":"Reef","tags":["sea","gold"]}\n',
'{"title":"Blank","tags":[]}\n',
]);
const rows = [];
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
rows.push(JSON.parse(line));
}
const result = rows
.slice(1)
.map((r) => ({
title: r.title,
shared: r.tags.filter((t) => rows[0].tags.includes(t)).length,
}))
.sort((a, b) => b.shared - a.shared);
console.log(JSON.stringify(result, null, 2));
Sample output
[
{
"title": "Reef",
"shared": 1
},
{
"title": "Dune",
"shared": 0
},
{
"title": "Blank",
"shared": 0
}
]