Solved

Angularjs loop

Posted on 2016-07-20
9
46 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
 
LVL 27

Expert Comment

by:BigRat
ID: 41723568
OK. If you want help with the code, post the data.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now