Meta_Data Meta_Data - 1 month ago 12
Javascript Question

Updating a dict in javascript with standard mathematical functions

This is a python code to create a dictionary and update it with standard mathematical functions. How can I do the same in javascript?

import math
Env = dict # An environment is a mapping of {variable: value}
env = Env()
env.update(vars(math)) # sin, cos, sqrt, pi, ..

Answer

You can read properties of Math with getOwnPropertyNames object and then reduce it:

const dict = {};
Object.getOwnPropertyNames(Math).reduce((r, k) => {
  r[k] = Math[k];
  return r;
}, dict);