Solved

Angularjs loop

Posted on 2016-07-20
9
53 Views
Last Modified: 2016-07-24
I am new to angular js. I had two web services which returns two set of json data. then I need to loop this two set of json data and match with one field, so I can get an indicator of it, How can I do it?

Thanks
0
Comment
Question by:oceanship
  • 5
  • 4
9 Comments
 

Author Comment

by:oceanship
ID: 41722098
should I use anjular.foreach()?
0
 
LVL 27

Expert Comment

by:BigRat
ID: 41722830
Yes and no. If you want to display any of the data then one binds the JSON data to HTML with ng-repeat. If it only a summary which is required the any form of loop (either via Angular or just plain old javascript) will do. Could be you a bit more specific?
0
 

Author Comment

by:oceanship
ID: 41723542
BigRat:
Thanks for the comments.
I am not display the data in html directly. I need modified my controller js file . In the controller js file, there are two rest web service call, I got two set of json data from web service, I need loop these two list (json data)  in the scope to find a match of the type code, so I can add a new indicator in one of the json data , then I can send the json data back to html.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 27

Expert Comment

by:BigRat
ID: 41723568
OK. If you want help with the code, post the data.
0
 

Author Comment

by:oceanship
ID: 41723721
Hi,
Here is my code:
sc.getTypes = function() {
                        
                                    return $http({
                                          headers: {"content-type": "application/json"},
                                         method: "GET",
                                         url: get_types_uri,
                                       })
                                     .then(function(response) {
                                           if(response.data.success) {
                                                 //get responses
                                                 sc.myList = response.data.data;
                                                 angular.foreach(sc.myList, function(value1,key){
                                                       console.log("value1: "+ value1);
                                                       angular.foreach(sc.userDetail, function(value2, key){
                  
                                                             
                                                             console.log("value2: "+ value2);
                                                             
                                                       },this)
                                                       
                                                 },this)
                                                       
                                                 } else {
                                                       sc.errors = ErrorHandlingSvc.processRESTErrorResponse(response.data.error);
                                                 }
                                                       
                                                 }, function(response) {
                                                       sc.errors = ErrorHandlingSvc.processSystemError(response.data);
                                                 });
                                               
                                }

Notes: I already get sc.userDetail from another web service call.
I got an error of this line: angular.foreach(sc.myList, function(value1,key){
the error is : Error: angular.foreach is not a function

Thanks
0
 

Author Comment

by:oceanship
ID: 41723947
HI, BigRat:
Here are the two jason data set:
 sc.myList= [{
            "typeCode": "A01",
            "typeCodeDesc": "Enrollment"
            },
            {
            "typeCode": "A02",
            "typeCodeDesc": "Legal"
            },
            {
            "typeCode ":"A03",
             "typeCodeDesc ":"Other",

            },
            {
            "typeCode ":"A04",
             "typeCodeDesc ":"Rules",

            }];
      



sc.userDetail=  [{
            "id": "124300",
            "docList": [{
                  "dFormat": "text",
                  "typeCode": "Default"
            }, {
                  "dFormat": "text",
                  "typeCode": "A01"
            }, {
                  "dFormat": "usMail",
                  "typeCode": "A02"
            }, {
                  "dFormat": "email",
                  "typeCode": "A03"
            }, {
                  "dFormat": "email",
                  "typeCode": "A04"
            }],
            "phoneNumber": "1234567891",
            "statusInfo": {
                  "serviceMessage": "SUCCESS",
                  "serviceStatus": "SUCCESS"
            }
      }]

I need loop this two set data based on the typeCode, if the typeCode match each other, then I need add one indicator in sc.myList for the dFormat. Is it doable?  

Thank you very much!
0
 
LVL 27

Expert Comment

by:BigRat
ID: 41724471
the error is : Error: angular.foreach is not a function

No, it's not. The correct function is forEach()

https://docs.angularjs.org/api/ng/function/angular.forEach
0
 
LVL 27

Accepted Solution

by:
BigRat earned 500 total points
ID: 41724484
function createList(typeList,userDocList) {
   var newList = [];
   angular.forEach(typeList,function(obj,index) {
      angular.forEach(userDocList,function(userDoc,index2) {
         if(obj.typeCode===userDoc.typeCode) {
            obj['dFormat']=userDoc.dFormat;
            newList.push(obj);
         };
      });
   });
   return newList;
}

var result = createList(sc.myList,userDetails[0].docLis)t;

Open in new window


I personally prefer not to change any input data but always create something new. It's then easier to compare.
0
 

Author Closing Comment

by:oceanship
ID: 41726945
Thanks BigRat
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question