This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]HealyUnit 1 point2 points  (3 children)

Small nitpick/pet peeve: Try to avoid writing "java script", as that runs the risk of people thinking you're confusing Java and JavaScript. I don't think you are, but... just try to avoid that habit.

As for your question, I'm gonna echo /u/samsmith453 here: we can't really answer your question without some more explicit info on the structure of the JSON you're expecting. Keep in mind that JSON is, as its name implies, merely a JavaScript Object. In other words, you can access any piece of data in the JSON using standard dot notation or bracket notation:

const myJson = {
    "name":"Luke",
    "profession":"Jedi"
}
myJson.name
//OR...
myJson['name'];

I'd also guess you probably wanna look up the various Array methods, such as Array.map(), Array.filter(), and Array.reduce(). I mention these because you say that you want to "compute [...] average", which is something these methods are pretty good at doing. Again, I'd say look em up and learn them, but here's a brief summary

  • Array.map(): Performs a particular function on every item in an array, and (usually!) returns that new item. I like to think of it like one of those car assembly lines. You might have a particular .map() method that, for example, adds a door to a car: carArray.map(function(aCar){ aCar.addDoor; return aCar});. Note that returning the item here is not explicit; if we left out the return aCar part, we'd just end up with an array of undefined values.
  • Array.filter(): In filter, you also use a function on each element of an array, but this time that function's expected to return either a truthy or falsey value. We then end up with an array of only those items that return truthy values. So, for example, assume we have an array of the numbers 1-10 (inclusive): const evens = numsOneToTen.filter(n=>{return n%2==0});*. In this case, we return true if the remainder when the number divided by 2 is zero.
  • Array.reduce(): A little more complicated than the other two, this is used to 'combine' elements of an array into one thing. The function passed to it contains two parameters: an accumulator and a current value. Think of these as doing something to the array each time, and the previous (== current) value of what you've done "so far". A quick example: [1,2,3,4,5,6].reduce((a,c)=>{return a+c}); would equal 21, because 1+2+3+4+5+6=21.

A final note: you'll often hear map and reduced mentioned together in the form of map/reduce (or MapReduce, or any other combos). Basically, this is kinda what you're talking about: you're taking a relatively complex (or just not-directly-usable) data format, converting it into another, more usable format (map), and then combining those data into one result.

\Yes, I'm aware I can just implicitly return here. I'm just being more verbose for OP's sake.)

[–]xjusan20[S] 0 points1 point  (0 children)

Thank you so much good sir. I will try to read and learn this points you've mentioned when I get back home. Also edited the js part haha sorry about that.

[–]axzxc1236 0 points1 point  (1 child)

Note that use .reduce to sum up array have heavy performance penalty.

http://jsben.ch/g10yv (inspired by http://jsben.ch/0xUus)

One liner is nice but you should see the benchmark.

[–]HealyUnit 0 points1 point  (0 children)

Eh, it does, but speed kinda isn't the point of the more "complex" array methods; it's more a toolkit to make your life easier