const FACTS = [ ['bird', 'isa', 'animal'], ['pet', 'isa', 'animal'], ['canary', 'isa', 'bird'], ['penguin', 'isa', 'bird'], ['animal', 'can-fly', 'no'], ['bird', 'can-fly', 'yes'], ['penguin', 'can-fly', 'no'], ['tweety', 'isa', 'canary'], ['tweety', 'isa', 'pet'], ['chilly', 'isa', 'penguin'], ]; const foo = x => ([ x, ...FACTS.flatMap(fact => ( x === fact[0] && 'isa' === fact[1] ? foo(fact[2]) : [] )) ]); // for testing; ignore const showAll = () => { ['animal', 'bird', 'canary', 'tweety', 'penguin', 'chilly'].forEach(x => { console.log(`${x} => ${foo(x)}`); }); };