开发者问题收集

角度计算 JSON 对象的父对象数量

2020-11-09
253

我有一个对象数组。每个对象都有一个名为“ parentLineIndex ”的属性。该属性的值是数组中哪个其他对象是所讨论对象的父对象的索引。

有时父对象可以有自己的父对象,因此对象的父对象数等于其父对象的父对象数加上其父对象的 ... tool.component.html:107 错误 TypeError:无法读取未定义的属性“parentLineIndex”

"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
}]
3个回答

您有一个平面数组并且没有嵌套,因此您可以使用 forEach 循环。

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

您可以使用过滤器:

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

@lissettdm @Eran。感谢您的回答,我选择了以下选项:

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;
        }
Daniel Xav De Oliveira
2020-11-16