Thursday, November 3, 2022

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

Issue

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 ?


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)



Answered By - GrafiCode
Answer Checked By - Willingham (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.