Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Angularjs loop

Posted on 2016-07-20
9
Medium Priority
?
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 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