Meta_Data Meta_Data - 1 year ago 86
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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download