0

I am coding in Html/Javascript, I have a JSON which contains lists : a list of "Intervenant" and in each item of "Intervenant" they have a list of course called "ListCoursa"

Here the JSON :

    {
  "Intervenant": [
  {
    "name": "John Doe",
    "dateOfBirth": "1980-01-02T00:00:00.000Z",
    "registered": true,
    "ListeCoursa": [
      {
        "cours": "Cardio",
        "Date": "2016-05-02T00:00:00.000Z",
        "Horaire": "9-11",
        "Semaine": "1"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-02T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "1"
      },
      {
        "cours": "Basic",
        "Date": "2016-05-03T00:00:00.000Z",
        "Horaire": "18-20",
        "Semaine": "1"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-04T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "1"
      },
      {
        "cours": "Cardio",
        "Date": "2016-05-09T00:00:00.000Z",
        "Horaire": "9-11",
        "Semaine": "2"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-09T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "2"
      },
      {
        "cours": "Basic",
        "Date": "2016-05-10T00:00:00.000Z",
        "Horaire": "18-20",
        "Semaine": "2"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-11T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "2"
      }
    ]
  }, 
  {
    "name": "John Doe2",
    "dateOfBirth": "1980-01-02T00:00:00.000Z",
    "registered": true,
    "ListeCoursa": [
      {
        "cours": "Cardio",
        "Date": "2016-05-01T00:00:00.000Z",
        "Horaire": "10-12",
        "Semaine": "10"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-02T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "10"
      },
      {
        "cours": "Basic",
        "Date": "2016-05-03T00:00:00.000Z",
        "Horaire": "18-20",
        "Semaine": "10"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-04T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "10"
      },
      {
        "cours": "Cardio",
        "Date": "2016-05-09T00:00:00.000Z",
        "Horaire": "9-11",
        "Semaine": "10"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-09T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "20"
      },
      {
        "cours": "Basic",
        "Date": "2016-05-10T00:00:00.000Z",
        "Horaire": "18-20",
        "Semaine": "20"
      },
      {
        "cours": "Flex",
        "Date": "1980-05-11T00:00:00.000Z",
        "Horaire": "14-16",
        "Semaine": "20"
      }
    ]
    
  },
  {
    "name": "John Doe3",
    "dateOfBirth": "1980-01-02T00:00:00.000Z",
    "registered": true,
    "ListeCoursa": [
      {
        "cours": "Circuit",
        "Date": "1980-05-22T00:00:00.000Z",
        "Horaire": "8-10",
        "Semaine":"210"
      },
      {
        "cours": "Cardio-pump",
        "Date": "1980-02-22T00:00:00.000Z",
        "Horaire": "16-18",
        "Semaine": "210"
      }
    ]
    
  }
  
]
}

I need to count the number of "cours" in 1 week. For this I've put a variable "Semaine" in the JSON which indicate in what week the course is given ("Semaine": "20" mean the course is given in week 20).

My question is how do I get all the "cours" from "Semaine: number" in javascript ?

I hope I was clear, Thanks.

Community
  • 1
  • 1
  • 1
    What have you tried? Where are you stuck? Do you know how to access an object property? Or how to write a loop? –  May 08 '16 at 13:31
  • Here is what I've done so far, I built a tab : ["John Doe Liste cours : ", "Cardio", "1", "Flex", "1", "Basic", "1", "Flex", "1", "Cardio", "2", "Flex", "2", "Basic", "2", "Flex", "2", "John Doe2 Liste cours : ", "Cardio", "10", "Flex", "10", "Basic", "10", "Flex", "10", "Cardio", "10", "Flex", "20", "Basic", "20", "Flex", "20"] Now I need to count the number of same Integer for each "John Doe", it will give me the number a course given in the week. – SANCIAUX Benoit May 08 '16 at 13:31
  • [This Q&A](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) gives pretty much all the information you need on accessing complex object/array data. –  May 08 '16 at 13:34
  • Add some formating to make it readable or create fiddler... – Vikash Pandey May 08 '16 at 13:40

1 Answers1

0

Here's a function that will get summarize all of the cours for each of your semaines:

var coursSummary = function(coursArray){
    var summaryArray = {}
  coursArray.forEach(function(cours){
    if (typeof summaryArray[cours.Semaine] != 'object'){
        summaryArray[cours.Semaine] = [cours.cours];
    } else {
        summaryArray[cours.Semaine].push(cours.cours);
    }
  });
  return summaryArray
}

// Begin Sample Code:

sampleCoursArray = [{
                "cours": "Cardio",
                "Date": "2016-05-02T00:00:00.000Z",
                "Horaire": "9-11",
                "Semaine": "1"
            }, {
                "cours": "Flex",
                "Date": "1980-05-02T00:00:00.000Z",
                "Horaire": "14-16",
                "Semaine": "1"
            }, {
                "cours": "Basic",
                "Date": "2016-05-03T00:00:00.000Z",
                "Horaire": "18-20",
                "Semaine": "1"
            }, {
                "cours": "Flex",
                "Date": "1980-05-04T00:00:00.000Z",
                "Horaire": "14-16",
                "Semaine": "1"
            }, {
                "cours": "Cardio",
                "Date": "2016-05-09T00:00:00.000Z",
                "Horaire": "9-11",
                "Semaine": "2"
            }, {
                "cours": "Flex",
                "Date": "1980-05-09T00:00:00.000Z",
                "Horaire": "14-16",
                "Semaine": "2"
            }, {
                "cours": "Basic",
                "Date": "2016-05-10T00:00:00.000Z",
                "Horaire": "18-20",
                "Semaine": "2"
            }, {
                "cours": "Flex",
                "Date": "1980-05-11T00:00:00.000Z",
                "Horaire": "14-16",
                "Semaine": "2"
            }]

 console.log(coursSummary(sampleCoursArray)); // Object containing of each of the 'cours' for each Semanine
Sators
  • 2,746
  • 1
  • 20
  • 26
  • Thank you, I just don't unerstand why did you recreate "sampleCoursArray" ? I have these information in my JSON. It seems like you recreate inside the code the database... I just began to learn javascript so correct me if I am wrong. Plus, how can I do if I want to put the function in my .js and how do I call it from the HTML ? Thank you, as I said I'm new here... – SANCIAUX Benoit May 08 '16 at 14:34
  • I only put the sampleCoursArray if you wanted to copy/paste and run the code standalone. You would only need the elements above the "Begin Sample Code" line. Your code does not example how you are storing your data array, so if you provide an example of how you are using your JSON in your javascript file, I might be able to help further... – Sators May 08 '16 at 15:53