-2

Help me get the array of all student's id and college id from the following JSON data. I want to have;

//to store id of all colleges.
var collegeid = [];

//store id of all students.
var studentsid = [];

//students id as per college:
var studentsIdInCollege1 = [];

var data = {  
  "college":[  
    {  
      "id":1,
      "school":"abc",
      "course":"cde",
      "students":[  
        {  
          "id":1,
          "name":"abc 123",
          "number":"156888"
        },
        {  
          "id":2,
          "name":"abc 123",
          "number":"156888"
        }
      ]
    },
    {  
      "id":2,
      "school":"xyz",
      "course":"lopl",
      "students":[  
        {  
          "id":3,
          "name":"abc 123",
          "number":"156888"
        },
        {  
          "id":4,
          "name":"abc 123",
          "number":"156888"
        }
      ]
    }
  ]
}

`

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
7guyo
  • 3,047
  • 1
  • 30
  • 31
  • 1
    Possible duplicate of [How to convert JSON object to an Typescript array?](https://stackoverflow.com/questions/39308423/how-to-convert-json-object-to-an-typescript-array) - there are some other duplicates around. – kabanus Apr 28 '18 at 07:34
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – JJJ Apr 28 '18 at 07:46

3 Answers3

2

For the colleges, just map the college array to the id of each object. For the students, use [].concat(... to spread over a map of the student IDs and get a flat object:

const input = {  
  "college":[  
    {  
      "id":1,
      "school":"abc",
      "course":"cde",
      "students":[  
        {  
          "id":1,
          "name":"abc 123",
          "number":"156888"
        },
        {  
          "id":2,
          "name":"abc 123",
          "number":"156888"
        }
      ]
    },
    {  
      "id":2,
      "school":"xyz",
      "course":"lopl",
      "students":[  
        {  
          "id":3,
          "name":"abc 123",
          "number":"156888"
        },
        {  
          "id":4,
          "name":"abc 123",
          "number":"156888"
        }
      ]
    }
  ]
}

const collegeIds = input.college.map(({ id }) => id);
console.log('collegeIds: ' + collegeIds);
const studentIds = [].concat(...input.college.map(({ students }) => students.map(({ id }) => id)));
console.log('studentIds: ' + studentIds);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thanks!. How can i get students id only for specific college. say college with id of 1. – 7guyo Apr 28 '18 at 07:44
  • You would select the appropriate college using `.find` and then use the same sort of `map` technique as with collegeIds over the students to get their ids. – CertainPerformance Apr 28 '18 at 07:46
  • Hi @CertainPerformance, i love your code , i dont understant this [].concat(...input.college.map(({ students }), three dots in concat – jvk Apr 28 '18 at 08:07
  • Am having difficulty in putting the ids in array, as; collegeIds: [1,2] I tried `collegeIds[]=collegeIds.push(input.college.map(({ id }) => id));` – 7guyo Apr 28 '18 at 08:59
  • @7guyo Don't put empty square brackets after variable names - it's invalid syntax. Do ``const college = input.college.find(({ id }) => id === 1); const studentIds = college.students.map(({ id }) => id);`` – CertainPerformance Apr 28 '18 at 09:13
  • @whoami Spread operator. It's a quick way of flat-mapping one level deep. – CertainPerformance Apr 28 '18 at 09:14
1

Use .map() to get college Ids:

let collegeIds = data.college.map(({ id }) => id);

Use .reduce() for student ids:

let studentIds = data.college.reduce((a, c) => (
  a.concat(c.students.map(({ id }) => id))
), []);

Use .reduce() to get an array of arrays where each array contains student ids:

let studentInEachCollege = data.college.reduce((a, c) => (
  a.push(c.students.map(({ id }) => id)), a
), []);

This will allow you to access each college students using index like studentInEachCollege[0] for first college, studentInEachCollege[1] for second and so on.

Demo:

var data = {  
  "college":[  
    {  
      "id":1,
      "school":"abc",
      "course":"cde",
      "students":[  
        {  
          "id":1,
          "name":"abc 123",
          "number":"156888"
        },
        {  
          "id":2,
          "name":"abc 123",
          "number":"156888"
        }
      ]
    },
    {  
      "id":2,
      "school":"xyz",
      "course":"lopl",
      "students":[  
        {  
          "id":3,
          "name":"abc 123",
          "number":"156888"
        },
        {  
          "id":4,
          "name":"abc 123",
          "number":"156888"
        }
      ]
    }
  ]
};

let collegeIds = data.college.map(({ id }) => id);
let studentIds = data.college.reduce((a, c) => (
  a.concat(c.students.map(({ id }) => id))
), []);
let studentInEachCollege = data.college.reduce((a, c) => (
  a.push(c.students.map(({ id }) => id)), a
), []);

console.log(collegeIds);
console.log(studentIds);
console.log(studentInEachCollege);

Docs:

Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
1

// College ids 
var cID = data.college.map((c) => { return c.id});

// Students
// It returns two arrays so you need to reduce it...
var students = data.college.map((c) => { return c.students});

var allStudents = students.reduce((a,b) => {return a.concat(b)}, []);

// IDs
var sID = allStudents.map((c) => { return c.id});
Merim
  • 1,283
  • 2
  • 21
  • 36