Solved

Angularjs loop

Posted on 2016-07-20
9
63 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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