0

I have got json data in a file (/static/models.json) like this in angular JS project ..

{
    "familyType": {
        "A650": [{
            "partID": "2630_H"
        }, {
            "partID": "2690_H"
        }, {
            "partID": "2680_H"
        }, {
            "partID": "2600_H"
        }, {
            "partID": "2620_H"
        }, {
            "partID": "2610_H"
        }],
        "S400": [{
                "partID": "S500_Aru"
            }, {
                "partID": "S600_Aru"
            }, {
                "partID": "S700_Aru"
            }, {
                "partID": "S800_Aru"
            }, {
                "partID": "S900_Aru"
            }, {
                "partID": "S100_Aru"
            }
        ]
    }
}

I have got below function with parameters passing into that method (Part_id1, Part_id2) and inside the function..I have got two family types (A650,S400) and i need to check whether the two input part_Id's(Part_id1, Part_id2) are belongs to same family or not (i.e either A650 or S400 )

If the two part_id's(Part_id1, Part_id2) are from different family types i need to alert user ... how can we traverse through json file and compare the part_ID's in both the family types ..

$scope.ValidateModelList = function(Part_id1, Part_id2, $http) {
     if ($scope.firTable.selectedRowsCount != 0) {
         $http.get("/static/models.json")
             .success(function(response) {
                 $scope.names = response;
             });
         // here i am storing json data in $scope.names object
         //  here i need to check that the part_ids with the family
     }
 }

Would any one please help on this that would be very grateful to me....

Many thanks in advance ....

Update

function ValidateModel(partIDlist){

             alert('2');
             var data = stubdata; // Stubdata is actual json data mentioned above
             var partIdResult = [];
             for (var key in data.familyType) {

                 angular.forEach(data.familyType[key], function (object) {

                     angular.forEach(partIDlist, function (partId) {

                         if (partId == object.partID) {
                             partIdResult.push({
                                 value: partId, family: key
                             });
                         }
                     });
                 });
             }

             for (var i = 0; i < partIdResult.length; i++) {
                // alert(partIdResult.length);
                //alert(partIdResult.family);
                alert(JSON.stringify(partIdResult))
                alert(partIdResult.family); // this is giving undefined value
             alert(partIdResult[0].family);

                 if (partIdResult.family === partIdResult[0].family){
                     alert('yes');
                     alert("part_id's are same");
                 }
                 else if (partIdResult.family !== partIdResult[0].family) {
                     alert("part_id's not same");
                 }
             }
         }

here I am passing a list like this ..

var partIDlist= ["S900_Aru", "S500_Aru", "2610_H"];

and I am getting alert(JSON.stringify(partIdResult)) partIdResult as like this

 [{"value":"S900_Aru","family":"S400"},
 {"value":"S500_Aru","family":"S400"},
 {"value":"2610_H","family":"A650"}]

I should get these three part_id's are not belong to same model .. instead of this I am getting part_id's are belongs to same

could you please help on this ... Many thanks in advance....

5 Answers 5

1

First succes is deprecated use .then instead. 2nd data are stored under response.data.

.then(function(response) {
     var data = response.data;
     var partId2Family = null, partId1Family = null;

     for(var key in data.familyType){
         angular.forEach(data.familyType[key], function(object){
              if(Part_id1 == object.partID){
                  partId1Family = key;
              }
              if(Part_id2 == object.partID){
                  partId2Family = key;
              }
         })
     }     
     if(partId1Family !== PartId2Family){
          // warning
     }         
}

This should do the trick.

Edit : array version

.then(function(response) {
     var data = response.data;
    var partIdResult = [];
     for(var key in data.familyType){
         angular.forEach(data.familyType[key], function(object){
              angular.forEach(partIds, function(partId){
                   if(partId == object.partID){
                        partIdResult.push({value:partId, family:key});
                   }
              });
         })
     }     
     for(var i = 1; i < partIdResult.length; i++){
         if(partIdResult[i].family !== partIdResult[0].family){
                //error
         }
     }      
}
Sign up to request clarification or add additional context in comments.

11 Comments

hi many thanks .. one more query.. if we got array list with list of part_id's..... this time how can we check whether all are belongs to same family or not ...
i edited and added the array version. Note : check underscorejs on the net, it's a really usefull library that provide utility function to write cleaner code when working with loops an array or object properties.
No problem they're just making you doing things already uglier with more risk of creating bugs and time consuming for nothing, that's their problem i guess. Let's say you can check it for your own personnal interest if you want.
is it applicable to this scenario also (even if i increased more familytypes in json file ...)(S400,A650 .. and so on..)
since familytype is not array i am getting error at this line 'var key in data.familyType' familtyType is undefined..
|
1
var partsList = {
    "familyType": {
        "A650": [{
            "partID": "2630_H"
        }, {
            "partID": "2690_H"
        }, {
            "partID": "2680_H"
        }, {
            "partID": "2600_H"
        }, {
            "partID": "2620_H"
        }, {
            "partID": "2610_H"
        }],
        "S400": [{
                "partID": "S500_Aru"
            }, {
                "partID": "S600_Aru"
            }, {
                "partID": "S700_Aru"
            }, {
                "partID": "S800_Aru"
            }, {
                "partID": "S900_Aru"
            }, {
                "partID": "S100_Aru"
            }
        ]
    }
};

function ValidateModelList(Part_id1, Part_id2){

    var part_id_1_family = getPartFamily(Part_id1);
  var part_id_2_family = getPartFamily(Part_id2)
  //alert(part_id_1_family + ", " + part_id_2_family);
    //console.log("part_id_1_family", part_id_1_family);
  //console.log("part_id_2_family", part_id_2_family);
    if(part_id_1_family != part_id_2_family) alert("parts are from different families!");
    else alert("parts from same family.")
}

function getPartFamily(part_id)
{
    var familyType = partsList.familyType;
  // loop through family type
    for(var family in familyType)
  {
    if(!familyType.hasOwnProperty(family)) continue;
    //loop through part objects in this family
    for(var j = 0; j < familyType[family].length; j++)
    {
        // check if parts id is in this array
        if(part_id == familyType[family][j].partID) return family;
    }
  }
    return false;
}

ValidateModelList("2630_H", "S900_Aru");

ValidateModelList("S600_Aru", "S900_Aru");

https://jsfiddle.net/Ladmwdvt/4/

1 Comment

hi many thanks .. if we got array list with list of part_id's..... this time how can we check whether all are belongs to same family or not..
0

Though not clear about your question, below is something how you would check.

Object.keys(type.familyType).forEach(function(key,index) {
   if(key === Part_id1 || key === Part_id2){
    //do something
     } else {
    //do something else
   }
});

A Fiddle to play with.

https://jsfiddle.net/2cgww6ec/1/

Comments

0

try this

var t = {
    "familyType": {
        "A650": [{
            "partID": "2630_H"
        }, {
            "partID": "2690_H"
        }, {
            "partID": "2680_H"
        }, {
            "partID": "2600_H"
        }, {
            "partID": "2620_H"
        }, {
            "partID": "2610_H"
        }],
        "S400": [{
                "partID": "S500_Aru"
            }, {
                "partID": "S600_Aru"
            }, {
                "partID": "S700_Aru"
            }, {
                "partID": "S800_Aru"
            }, {
                "partID": "S900_Aru"
            }, {
                "partID": "S100_Aru"
            }
        ]
    }
};

search("2630_H","2620_H");

function search(ida,idb) {
  $.each(t['familyType'], function(a, b) {
    var str = JSON.stringify(b);
    if(str.indexOf(ida)>=0 && str.indexOf(idb) >=0){
      alert("same family :  " + a);
      return;
    } 
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p> search("2630_H","2620_H") will called <p> 

Comments

0

Try this:

var obj = {
    "familyType": {
        "A650": [
            {
                "partID": "2630_H"
            }, {
                "partID": "2690_H"
            }, {
                "partID": "2680_H"
            }, {
                "partID": "2600_H"
            }, {
                "partID": "2620_H"
            }, {
                "partID": "2610_H"
            }
        ],
        "S400": [
            {
                "partID": "S500_Aru"
            }, {
                "partID": "S600_Aru"
            }, {
                "partID": "S700_Aru"
            }, {
                "partID": "S800_Aru"
            }, {
                "partID": "S900_Aru"
            }, {
                "partID": "S100_Aru"
            }
        ]
    }
};

for (familyid in obj.familyType){
    obj.familyTyle[familyid].forEach(function(key){
        if(key.partID == Part_id1) family1 = familyid;
        if(key.partID == Part_id2) family2 = familyid;
    });
}

if(family1 === family2){
    alert('parts are same');
}

1 Comment

Hi in future more family types will add so in that case i cannot hard code each family like this "obj.familyType.A650." ....... is there any generic way ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.