skip to Main Content

the first json is coming from the API in get this data by making a http call

[
    {
        "clientid": 1,
        "clientname": "facebook",
        "forecasted": 18,
    },
    {
        "clientid": 2,
        "clientname": "youtube",
        "forecasted": 83,
    }
]

i create the second json to update the image on the first json

 [
        {
            "clientid": 1,
            "clientname": "facebook ",
            "img":"images/21747.jpg"
        },
        {
            "clientid": 2,
            "clientname": "youtube",
            "img": "images/youtube.svg"
        },
    ]

now i need to macth both json base on the same id, and fill the first json that i coming from the api with images of the second json

app.factory('myFactory', function($http){
  return{

    client: function(callback){
        $http({
          method: "GET",
          url: "http://www.erek.co.za/api/..",
          cache:true
        }).then(callback);
    },  
    list: function(callback){
        $http({
          method: "GET",
          url: "data/Client.json",
          cache:true
        }).then(callback);
    } 
  };
});
app.controller('myController', function ($scope, myFactory) {
          myFactory.client(function(response){
              var data = response.data;
              $scope.myClients = data;
          })
          myFactory.list(function (response) {
            var data = response.data;
            $scope.dark = data;
          })
        });

html

<div ng-repeat="client in myClients>
<a href="#{{client.clientid}}" 
  <div class="client-project-panel">
      <div class="client-logo-container">
          <div class="client-logo" >
              <img ng-src="{{dark[$index].img}}" alt="{{client.clientname}}">
           </div>
          <div class="project-forecasted">
             <h2 class="forecasted-value">{{client.forecasted}}%</h2>
             <p>Complete</p>
          </div>
      </div>
  </div>
</a>

2

Answers


  1. You can use Object.assign method.

    let array1=[ { "clientid": 1, "clientname": "facebook", "forecasted": 18, }, { "clientid": 2, "clientname": "youtube", "forecasted": 83, } ]
    let array2 = [ { "clientid": 1, "clientname": "facebook ", "img":"images/21747.jpg" }, { "clientid": 2, "clientname": "youtube", "img": "images/youtube.svg" } ]
     
    var expected = array1.map(a => Object.assign(a, array2.find(b => b.clientid == a.clientid)));
    console.log(expected);
    Login or Signup to reply.
  2. myFactory.client(function(response){
              var data = response.data;
              var clientsData = data;
              // after you fetch the clients get list of images
              myFactory.list(function (response) {
                var data = response.data;
                var clientImages = {}
                for(var i=0;i<data.length;i++){
                    clientImages[data[i].clientid] = data[i].img
                }
                $scope.dark = clientImages
                $scope.myClients = clientsData
              })
    
    })
    

    In your template just refer to {{dark[client.clientid]}}

    You might want to wrap your html in ng-if="myClients" so that the content is shown only after the data is fetched

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search