Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is Python list(), filter(), lambda equivalent to Javascript?

This is script in python. I want to achieve the same goal with Javascript. So the script is that

my_list = [18, 19, 20, 27, 28, 29, 38, 39, 40]

new_list = list(filter(lambda x: x + 10 in my_list or x - 10 in my_list, my_list))

What is equivalent of list(), filter(), lambda in this situation to convert Python to Javasript ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It can be converted to JavaScript using Array.filter() and Array.includes().

const my_list = [18, 19, 20, 27, 28, 29, 38, 39, 40]

const new_list = my_list.filter(x => my_list.includes(x+10) || my_list.includes(x-10))

console.log(new_list)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading