Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular count json对象的父对象数_Javascript_Angular - Fatal编程技术网

Javascript Angular count json对象的父对象数

Javascript Angular count json对象的父对象数,javascript,angular,Javascript,Angular,我有一个对象数组。每个对象都有一个名为“parentLineIndex”的属性。属性的值是数组中的其他对象是所讨论对象的父对象的索引 有时父对象可以有自己的父对象,因此对象将有其父对象加上其父对象的父对象计数,以此类推 如果对象/父对象没有父对象,则“parentLineIndex”==-1 我需要确定每个对象的父对象数。我的想法是看看“parentLineIndex”=-1。如果没有,则添加一个。使用递归函数。我已经试过了,但是当我递归调用函数时,它说参数是未定义的。 tool.compone

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

有时父对象可以有自己的父对象,因此对象将有其父对象加上其父对象的父对象计数,以此类推

如果对象/父对象没有父对象,则“parentLineIndex”==-1

我需要确定每个对象的父对象数。我的想法是看看“parentLineIndex”=-1。如果没有,则添加一个。使用递归函数。我已经试过了,但是当我递归调用函数时,它说参数是未定义的。 tool.component.html:107错误类型错误:无法读取未定义的属性“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
}]

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

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

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

iterate(array: any[]): number {
  let count = 0;
  array.forEach(obj => {
    if (obj.parentLineIndex && obj.parentLineIndex !== -1) {
      count++;
    }
  });
  console.log(count);
  return count;
}
您可以使用过滤器:

const count = iterate.filter(item => item.parentLineIndex && item.parentLineIndex !== -1).length;
您可以使用过滤器:

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

@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;
        }
indexG=0;
$scope.increase=功能(项目){
var childHasParent=documentsLines.find(element=>element.lineIndex==item);
if(childHasParent!=null){
$scope.count[indexG]=$scope.count[indexG]==未定义?1:$scope.count[indexG]+1;
var exist=childHasParent.parentLineIndex!=-1;
如果(存在){
增加(childHasParent.parentLineIndex);
}
}否则{
指数g=0;
log($scope.count);
返回$scope.count;
}
}
$scope.myFunction=function(){
documentsLines=json[0]。可编辑的DocumentLines;
$scope.count=[];
对于(i=0;i
@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;
        }
indexG=0;
$scope.increase=功能(项目){
var childHasParent=documentsLines.find(element=>element.lineIndex==item);
if(childHasParent!=null){
$scope.count[indexG]=$scope.count[indexG]==未定义?1:$scope.count[indexG]+1;
var exist=childHasParent.parentLineIndex!=-1;
如果(存在){
增加(childHasParent.parentLineIndex);
}
}否则{
指数g=0;
log($scope.count);
返回$scope.count;
}
}
$scope.myFunction=function(){
documentsLines=json[0]。可编辑的DocumentLines;
$scope.count=[];
对于(i=0;i