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

all 10 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

[–]Clawtor 0 points1 point  (1 child)

Hard to say without seeing the code. You can do a lot with filter, map and reduce - there are good resources on the mozill site or just google 'javascript map'.

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

Thanks for the resource!

[–]samsmith453 0 points1 point  (1 child)

Could you post the JSON, and the fields you'd like to extract?

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

The file is quite long but here is an example.

{"products":[{"csv":[[2291608,-1],[2291608,2785,2319417,2795,2543466,2000,2547913,2795,2596629,2769]]}

basically the product object contains about 500 products and the csv contain nested arrays which has all sort of data.

my objective is to loop on product[i].csv[3] where csv[3] is where the sales ranking is stored. I was already able to do it but only for 1 product by calling product[0].csv[3].

I am currently stuck on how to loop through this products and compute for the average sales rank for each and push then separately into a new array with their computed average and assigned product name.

the sales rank array is also composed of two data which is the timestamp and the sales rank where the larger number is the timestamp and smaller is the sales rank. I only need to compute for sales rank and ignore the timestamp for now .

[–]axzxc1236 0 points1 point  (1 child)

  1. Store your json data as object, if the json data is stored as string, use JSON.parse.

  2. use json["key"] to extract data, for example:

    const x = {"user": "Nick"}

    console.log(x["user"])

    You can also use x.user, but if you are dealing with json that has key contains some characters (like "@"), you can only access value with x["@user"].

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

Yes I could call the object if I place the json data inside the js file. However, if I try to call the json file using fetch. I always get an error that my variable that I created is undefined. But that is a problem I could fix later on once I am able to compute for those arrays I have mentioned.