?
Solved

Angularjs loop

Posted on 2016-07-20
9
Medium Priority
?
67 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
You have this traditional web application that refreshes the whole page each time you navigate and you think it's a good time to convert it to a Single-Page approach. What should you know in advance? Read on, I'll guide you through it.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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