Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Below is my json structure:

$scope.dataList = [{
   CompanyName: null,
   Location: null,
   Client: {
      ClientId: 0,
      ClientName: null,
      Projects:{
         Id: 0,
         Name: null,
      }
   }
}];

I have this data in dataList scope variable :

[{
   CompanyName: XXXX,
   Location: California,
   Client:{
      ClientId: 1,
      ClientName: John Cena,
      Projects:{
         Id: 1,
         Name: Abc,
         }
      }
}]

Now in the above record I need to find by company name and location and add Client array to matched company.

Now based on button click event i am getting 1 or more another record from http call like below:

[{
   CompanyName: XXXX,
   Location: California,
   Client:[{
      ClientId: 2,
      ClientName: Undertaker,
      Projects:{
         Id: 2,
         Name: Pqr,
      }
   }]
}]

Now i want to append this new client data to my dataList object for company XXXX and for location California.

This is how i am trying but I am getting errors:

$http.get("Url")
    .then(function(data) {
        for (var i = 0; i < data.length; i++) // when there will be more than 1 records in response i get from http call
                {
                        var result = $.grep($scope.dataList, function (e) 
                        {
                            //find records from dataList by Company name and Location
                            return (e.CompanyName == data[i].CompanyName) && (e.Location == data[i].Location);
                        });
                        //Push client Id 2 record in dataList
                        result.Client.push(data[i]);// error:result.Client is undefined
                        console.log(result);
                }
    });

Can anybody please help me with this??

share|improve this question
    
$scope.dataList[0].Client is an object, not an array. You are trying to push something into an object. – user2415266 yesterday
    
Additionally you are not checking if there actually is a record with matching CompanyName and Location. That's the error it gives you, because your $.grep didnt find anything and therfor result is undefined. – user2415266 yesterday
    
@user2415266 I am getting record in result variable.i have checked in console – Learning yesterday
    
Please, read this (how to ask) and this (mcve) before asking, as those will help you get more and better answers from the community. Edit your question, post logs, details, server errors, etc, anything and everything you have of information, we dont need to guess on. This way, we can actually point in a better direction for you. – Bonatti yesterday
1  
@Bonatti I have posted every possible detail that I have with me.can you please tell me what is the thing which is missing or unclear to you.Asking questions are now becoming difficult day by day on SO as some very few users always find some flaw in question.sorry I am not being rude but faced this situation before when in some of the question I posted every possible details then I was told that please shorten your question etc etc... :) – Learning yesterday

You could use a check and move the content of Client into an array, before pushing to result.Client

if (!Array.isArray(result.Client)) { // check if not an array
    result.Client = [result.Client]; // create one with the actual content
}
result.Client.push(data[i]);
share|improve this answer
    
Not getting new client record inside client array.new client array is pushed outside of Client array – Learning yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.