Hi All,
Another newbie JS question concerning differences in Observable vs. ‘traditional’ JavaScript…
I’m following along the tutorials from learnjsdata, and currently am working on the section on iterating.
The example defines data, then defines count
as a variable with the initial value of 0
:
var data = [
{"city":"seattle", "state":"WA", "population":652405, "land_area":83.9},
{"city":"new york", "state":"NY", "population":8405837, "land_area":302.6},
{"city":"boston", "state":"MA", "population":645966, "land_area":48.3},
{"city":"kansas city", "state":"MO", "population":467007, "land_area":315}
];
var count = 0;
data.forEach(function(d) {
count += 1;
});
console.log(count);
If I try this in Observable without first creating a function, I get an error on var
about an unexpected token. If I try to set count
in a separate cell, I get an error about assignment to a constant variable. If I transform this into a function in a manner similar to an example in Mike’s tutorial on flat arrays, I get a function, but I can’t seem to get it to return an actual value. [That is, the example suggests I use console.log(data.length);
, but the console.log()
function doesn’t seem to be relevant in Observable.]
I see that functions can return values, such as in Mike’s Introduction to Asynchronous Iteration, where he uses let
instead of var
and then defines another const
, but I can’t see how to make it work with this tutorial. The method of defining and mutating variables with functions in Observable still confuses me a bit.
How best to understand how to add in a variable to a function and when to separate it? And how to ‘print’ the value for a function in Observable?
Thanks in advance for your help!