TF.js: How to scale and normalize values in TensorFlow

I have a Tensor that I initialize as follows (thank @Dennis for you help with this):

//let w = tf.rand([2,1], () => Math.random());
let w = tf.randomUniform([2,1]);


  dtype: float32
  rank: 2
  shape: [2,1]

What if I want to scale the values so that each one is in [0, 1] and sum to 1 while maintaining their relative weights?

In regular JavaScript I would apply a map function and do something like this:

let sumOfWeights = arr.reduce((acc, w) => acc + w, 0); => (w /= sumOfWeights));

But how do I do this in TensorFlow?

Alternatively, how can I generate a tensor of random numbers, summing to 1, without going through the above transformations/operations?

EDIT: I think I found a solution below, unless there is a better way to do it.

let w = tf.randomUniform([2,1]);
let sum = tf.sum(w);
w = tf.div(w, sum);