How to add multiple values from different arrays into an dropdown?

Course Queries Syllabus Queries 3 years ago

4.23K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago


I am trying to display data from an api into a dropdown (Department Full Name). I can display one value but not multiple values. Can you please tell me where am I going wrong?

From the json array I want to get values into select dropdown. The values I want to get into dropdown are shortNamesemester and section. I can display only shortName but not other 2 values.

index.controller.js

   $http.get('https://student.herokuapp.com/department/')
        .success(function(response){

            $scope.deptDetails=response.department;
         //   console.log($scope.deptDetails);
            deptLen=response.department.sections.length;
            console.log(deptLen);

            for(var i=0;i<deptLen;i++){

                $scope.deptDetails.push({"_id":response.department._id,
                                                "shortName":response.department.shortName,
                                                "section":response.department.sections[i].section,
                                                "semester":response.department.sections[i].semester});
            }

        });

Json array

    {
 "department": {
"_id": "585955afce1a5e0011a2ecf5",
"collegeId": "58295efb836f8611d5e3e4cb",
"fullName": "Compcsuter Science",
"shortName": "CS",
"numberOfSemesters": "8",
"semesterYearScheme": "Semester",
"programName": "UG",
"numberOfSections": "1",
"sections": [
       {
   "_id": "585fb137bf29882207752552",
   "collegeId": "585e53c6729e5c2214b97a0f",
   "departmentId": "585fb137bf29882207752546",
   "semester": 4,
   "section": "C",
   "syllabus": [],
   "teachers": [],
   "students": [],
   "createdBy": "58332b90a7986c09b7e81980",
   "createdAt": "2016-12-25T11:44:55.445Z",
   "__v": 0
        }
    ]
0 views
0 shares

profilepic.png
manpreet 3 years ago

You can do this,

app.controller("dobController", ["$https://forum.tuteehub.com/tag/scope">scope", '$http',
      https://forum.tuteehub.com/tag/function">function($https://forum.tuteehub.com/tag/scope">scope, $http) {
         $https://forum.tuteehub.com/tag/scope">scope.displayValues = [];
         $http.get('test.json').then(https://forum.tuteehub.com/tag/function">function(https://forum.tuteehub.com/tag/response">response){
           $https://forum.tuteehub.com/tag/scope">scope.data = https://forum.tuteehub.com/tag/response">response.data;
           console.log(https://forum.tuteehub.com/tag/response">response.data.department);
           $https://forum.tuteehub.com/tag/scope">scope.displayValues.push(https://forum.tuteehub.com/tag/response">response.data.department.shortName);
            https://forum.tuteehub.com/tag/response">response.data.department.sections.forEach(https://forum.tuteehub.com/tag/function">function(https://forum.tuteehub.com/tag/key">key){
               $https://forum.tuteehub.com/tag/scope">scope.displayValues.push(https://forum.tuteehub.com/tag/key">key.section);
               $https://forum.tuteehub.com/tag/scope">scope.displayValues.push(https://forum.tuteehub.com/tag/key">key.semester);
            })

         });

      }
    ]);

EDIT

  https://forum.tuteehub.com/tag/response">response.data.department.sections.forEach(https://forum.tuteehub.com/tag/function">function(https://forum.tuteehub.com/tag/key">key){
           $https://forum.tuteehub.com/tag/scope">scope.displayValues.push(https://forum.tuteehub.com/tag/response">response.data.department.shortName + " "+ https://forum.tuteehub.com/tag/key">key.section + " "+https://forum.tuteehub.com/tag/key">key.semester);

     })

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum