Daily Tips - Some simple Expressions πŸŒ“

Hi there,

Today, we’re going to convert several commonly used python expressions into JavaScript. This may be useful when you need to convert some client-side callbacks.

True if something or something_else else False

\qquad \Updownarrow

something || something_else ? true : false

all(_list)
any(_list)

\qquad \Updownarrow

_list.every(Boolean)
_list.some(Boolean)

'item' in ['item1', 'item2', 'item3']
'item' not in ['item1', 'item2', 'item3']

\qquad \Updownarrow

['item1', 'item2', 'item3'].includes('item')
!['item1', 'item2', 'item3'].includes('item')

[i for i in range(1, 10, 2)]

\qquad \Updownarrow

Array.from(Array(5).keys(), n => 2 * n + 1)

i * 10**power

\qquad \Updownarrow

power < 0 ? (i / 10 ** -power) : (i * 10 ** power)

def func(*args):
    retrun args[0] + args[1]
func(lambda x: x[0] +x[1])

\qquad \Updownarrow

const func = (...rest) => rest[0] + rest[1]

list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))

\qquad \Updownarrow

[1, 2, 3, 4, 5].map(n => n ** 2)

','.join(_list)

\qquad \Updownarrow

_list.join(',')

[i for i in _list if i % 2 != 0]
filter(lambda x: x % 2 != 0, _list)

\qquad \Updownarrow

_list.filter(item => item % 2 != 0)

np.ravel(matrix)
matrix.ravel()

\qquad \Updownarrow

matrix.flat(Infinity)

np.max(matrix)
matrix.max()

\qquad \Updownarrow

Math.max(...matrix.flat(Infinity))

params = {j: _list2[i] for i, j in enumerate(_list1)}
params = {i: j for i, j in zip(_list1, _list2)}

\qquad \Updownarrow

const params = new Map(_list1.map((i, j) => [i, _list2[j]]));

These are what I have just used, I hope someone else can also add them.


Hope this helps you. XD

Keywords: Client-side Callbacks, JS

Other Daily Tips series:
Daily Tips - If I have a Bunch of Triggers
Daily Tips - Share the Function
Daily Tips - How many Ways to share Dash in a company?
Daily Tips - Give your Component a Name
Daily Tips - Share Dash to Social Media
Daily Tips - Double-click to open Dash
Daily Tips - What rows of data have I modified?
Daily Tips - Write the Simplest Client-side Callback
Daily Tips - Some simple Expressions
Daily Tips - IoT? Real Real-time Data and Live Update
Daily Tips - Which formatter is your favorite?
Daily Tips - Convert a React Component to Dash
Daily Tips - Real-time Logs on Dash
Daily Tips - Control Permissions Precisely

5 Likes