0
votes

Comptage angulaire du nombre de parents pour un objet json

J'ai une panoplie d'objets. Chaque objet a une propriété appelée " parentLineIndex ". La valeur de la propriété est l'index dont l'autre objet du tableau est le parent de l'objet en question.

Parfois, un parent peut avoir son propre parent, ainsi un objet aura un décompte de parents de son parent plus ses parents parents et ainsi de suite.

Si un objet / parent n'a pas de parent, alors " parentLineIndex " == -1.

"editableDocumentLines": [
{
  "text": "AGREEMENT",
  "bulletText": null,
  "page": 1,
  "lineIndex": 0,
  "parentLineIndex": -1
},
{
  "text": "We will provide the insurance described in this policy in return for the premium and compliance with all applicable provisions of this policy.",
  "bulletText": null,
  "page": 1,
  "lineIndex": 1,
  "parentLineIndex": 0
},
{
  "text": "DEFINITIONS",
  "bulletText": null,
  "page": 1,
  "lineIndex": 2,
  "parentLineIndex": -1
},
{
  "text": "In this policy, \"you\" and \"your\" refer to the \"named insured\" shown in the Declarations and the spouse if a resident of the same household. \"We\", \"us\" and \"our\" refer to the Company providing this insurance.",
  "bulletText": "A.",
  "page": 1,
  "lineIndex": 3,
  "parentLineIndex": 2
},
{
  "text": "In addition, certain words and phrases are defined as follows:",
  "bulletText": "B.",
  "page": 1,
  "lineIndex": 4,
  "parentLineIndex": 2
},
{
  "text": "\"Aircraft Liability\", \"Hovercraft Liability\", \"Motor Vehicle Liability\" and \"Watercraft Liability\",",
  "bulletText": "1.",
  "page": 1,
  "lineIndex": 5,
  "parentLineIndex": 4
},
{
  "text": "subject to the provisions in b. below, mean the following:",
  "bulletText": null,
  "page": 1,
  "lineIndex": 6,
  "parentLineIndex": 5
},
{
  "text": "Liability for \"bodily injury\" or \"property damage\" arising out of the:",
  "bulletText": "a.",
  "page": 1,
  "lineIndex": 7,
  "parentLineIndex": 6
},
{
  "text": "Ownership of such vehicle or craft by an \"insured\";",
  "bulletText": "(1)",
  "page": 1,
  "lineIndex": 8,
  "parentLineIndex": 7
},
{
  "text": "Maintenance, occupancy, operation, use, loading or unloading of such vehicle or craft by any person;",
  "bulletText": "(2)",
  "page": 1,
  "lineIndex": 9,
  "parentLineIndex": 7
}]

J'ai besoin de déterminer le nombre de parents pour chaque objet. Ma pensée est de regarder si " parentLineIndex " == -1. et sinon, ajoutez-en un. Utilisation d'une fonction récursive. Je l'ai essayé mais lorsque j'appelle la fonction de manière récursive, cela dit que les paramètres ne sont pas définis. tool.component.html: 107 ERROR TypeError: Impossible de lire la propriété 'parentLineIndex' d'undefined

    iterate(index, array) {
    let count = 1;
    this.index2 =index;
    this.array2 =array;
  if (this.array2[this.index2].parentLineIndex !== '-1') {
        count++;
        console.log("passed");
        this.iterate(this.array2[this.index2].parentLineIndex, this.array2);

    } else {
        return count;
    }


}


0 commentaires

3 Réponses :


1
votes

Vous avez un tableau plat et non imbriqué, vous pouvez donc forEach boucle forEach .

iterate(array: any[]): number {
  let count = 0;
  array.forEach(obj => {
    if (obj.parentLineIndex && obj.parentLineIndex !== -1) {
      count++;
    }
  });
  console.log(count);
  return count;
}


0 commentaires

1
votes

Vous pouvez utiliser le filtre:

const count = iterate.filter(item => item.parentLineIndex && item.parentLineIndex !== -1).length;


0 commentaires

0
votes

@lissettdm @Eran. Merci pour vos réponses, je suis allé avec ce qui suit:

  indexG = 0;

        $scope.increase = function(item) {
            var childHasParent = documentsLines.find(element => element.lineIndex == item);
            if (childHasParent != null) {

                $scope.count[indexG] = $scope.count[indexG] == undefined ? 1 : $scope.count[indexG] + 1;

                var exist = childHasParent.parentLineIndex != -1;

                if (exist) {
                    increase(childHasParent.parentLineIndex);
                }
            } else {
                indexG = 0;
                console.log($scope.count);
                return $scope.count;
            }
        }

        $scope.myFunction =function() {

            documentsLines = json[0].editableDocumentLines;
            $scope.count = [];


            for (i = 0; i < documentsLines.length; i++) {
                item = documentsLines[i];

                if (item.parentLineIndex != -1) {
                    indexG = item.lineIndex;
                    increase(item.parentLineIndex);

                }

            }
            console.log(count)
            return $scope.count;
        }


0 commentaires