Created
December 11, 2019 19:22
-
-
Save froddd/bc74b477daf89822a9dffcf2bd4913d8 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/qivaqog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
const ids = ['a', 'd', 'e']; | |
const products = [ | |
{ id: 'a', related: ['b', 'c']}, | |
{ id: 'b', related: ['c']}, | |
{ id: 'd', related: ['b']}, | |
{ id: 'e', related: ['a', 'b']} | |
]; | |
const result = products | |
.filter(product => ids.includes(product.id)) | |
.reduce((accu, product) => { | |
product.related.forEach(p => { | |
if (!ids.includes(p) && !accu.includes(p)) accu.push(p); | |
}) | |
return accu; | |
}, []) | |
console.log(result); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const ids = ['a', 'd', 'e']; | |
const products = [ | |
{ id: 'a', related: ['b', 'c']}, | |
{ id: 'b', related: ['c']}, | |
{ id: 'd', related: ['b']}, | |
{ id: 'e', related: ['a', 'b']} | |
]; | |
const result = products | |
.filter(product => ids.includes(product.id)) | |
.reduce((accu, product) => { | |
product.related.forEach(p => { | |
if (!ids.includes(p) && !accu.includes(p)) accu.push(p); | |
}) | |
return accu; | |
}, []) | |
console.log(result);</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ids = ['a', 'd', 'e']; | |
const products = [ | |
{ id: 'a', related: ['b', 'c']}, | |
{ id: 'b', related: ['c']}, | |
{ id: 'd', related: ['b']}, | |
{ id: 'e', related: ['a', 'b']} | |
]; | |
const result = products | |
.filter(product => ids.includes(product.id)) | |
.reduce((accu, product) => { | |
product.related.forEach(p => { | |
if (!ids.includes(p) && !accu.includes(p)) accu.push(p); | |
}) | |
return accu; | |
}, []) | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment