Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 比较angularjs中的对象数组_Javascript_Angularjs_Json - Fatal编程技术网

Javascript 比较angularjs中的对象数组

Javascript 比较angularjs中的对象数组,javascript,angularjs,json,Javascript,Angularjs,Json,我有一个从另一个URL获得的json(示例数据) var data = [ { id: 1, name: 'ravi', parentid: 0 }, { id: 2, name: 'raj', parentid: 1 }, { id: 3, name: 'ram', parentid: 1 }, { id: 4, name: 'raja', parentid: 0 }, { id: 5, name: 'raju', parentid: 4 },        { id: 6, name: 'd

我有一个从另一个URL获得的json(示例数据)

var data = [
{ id: 1, name: 'ravi', parentid: 0 },
{ id: 2, name: 'raj', parentid: 1 },
{ id: 3, name: 'ram', parentid: 1 },
{ id: 4, name: 'raja', parentid: 0 },
{ id: 5, name: 'raju', parentid: 4 },       
{ id: 6, name: 'dinesh', parentid: 4 }  
];
当我在angular中获得成功消息时,我想比较具有id的数据 剩余数据的
parentid
为零的
parentid
。我尝试了以下代码,但无法继续执行更多操作

$http.get('URL')
    .success(function(data) {
        var categories = data;
        for(var i =0;i<=categories.length;i++)
        {
            if(typeof categories[i] != "undefined" && categories[i].parentId == 0)
            {
                $scope.cat.push(categories[i]);
            }   
            if($scope.cat[i].parentid == categories[i].id)
            {
                $scope.content.push(categories[i]);
            }           
        }

    });

我面临以下三个问题:

  • if(typeof categories[i]!=“undefined”&&categories[i].parentId==0)
    中,您正在比较
    parentId
    ,但它从来都不是,因为您的属性名是
    parentId
    (请检查大小写)。因此,第一个
    if
    循环从未执行,这就是为什么
    $scope.cat
    仍然未定义的原因
  • 解决方案:更正
    parentId

  • 在,
    if($scope.cat[i].parentid==categories[i].id)
    中,您正在比较父元素的父id与子元素的id
  • 解决方案:
    if($scope.cat[i].id==categories[i].parentid)

  • 您对两个长度不同的列表项使用相同的迭代器
  • 解决方案:

     var categories = data;
    
     // fill $scope.cat with elements having parent id = 0, so this list will contain all the parent elements
     $scope.cat = categories.filter(function(category) {
         return category && category.parentid === 0
     });
     $scope.content = [];
     // fill $scope.content with elements whose parent exist in $scope.cat (the parent list craeted above)
     categories.forEach(function(category) {
         if ($scope.cat.filter(function(cat) {
                 return category.parentid === cat.id
             }).length > 0)
             $scope.content.push(category);
     });
    

    示例

    试试这样的方法

    替换

    if($scope.cat[i].parentid == categories[i].id)
                {
                    $scope.content.push(categories[i]);
                }   
    


    对于
    $scope.cat[i]
    ,您会得到
    未定义的
    ,因为推送到
    $scope.cat
    的代码即使在将
    parentId
    更正为
    parentId
    后也不会始终执行

    if(typeof categories[i] != "undefined" && categories[i].parentid == 0)
    {
                    $scope.cat.push(categories[i]);
    }  
    
    对于数据数组的某些值,此条件将不为真。因此,不会将任何内容推送到
    $scope.cat

    请记住,您的第二个
    if
    语句紧跟在第一个
    if
    语句之后。因此,有时您将没有
    $scope.cat[i]

    对于所有这些值,代码将不会执行,因为它们不通过条件

    { id: 2, name: 'raj', parentid: 1 },
    { id: 3, name: 'ram', parentid: 1 },
    { id: 5, name: 'raju', parentid: 4 },       
    { id: 6, name: 'dinesh', parentid: 4 } 
    
    因为
    父id
    不是
    0

    因此,这些是导致
    未定义的值

    您可以更改为:

    var categories = data;
        for(var i =0;i<=categories.length;i++)
        {
            if(typeof categories[i] != "undefined" && categories[i].parentid ==0)
            {
                $scope.cat.push(categories[i]);
            }
    
        }
        //you will already have you $scope.cat filled then loop through it 
        //checking with the categories array too.
    
        for(var j=0;j<$scope.cat.length;j++)
        {
           for(var k=0; k<categories.length; k++){
             if($scope.cat[j].parentid==categories[k].id)
             {
               $scope.content.push(categories[k]);
             }
           }
        }
    
    var categories = data;
    
    categories = categories
    .filter( (category)=>{
       return (category !== []._);  //removes all undefineds in the array
    })
    .map((category)=>{
      //checks each category from data if is parent_Id is 0 and pushes it to cats array
      //by the end of the operation. the cats array will have some data
      (category.parentId == 0)? ($scope.cats.push(category) , return ) : return;
    })
    var getContent =(cats,categories)=>{
      var content=[];
      for(var j=0;j<cats.length;j++)
      {
         for(var k=0; k<categories.length; k++){
           if(cats[j].parentid==categories[k].id)
           {
             content.push(categories[k]);
           }
         }
      }
      return content;
    }
    $scope.content = getContent($scope.cats,categories);
    
    var类别=数据;
    对于(var i=0;i{
    var内容=[];
    
    对于(var j=0;j
    istill,我得到了一个错误:$scope.cat[i]未定义我看不到代码中定义了
    $scope.cat
    的任何地方。它应该是什么?即使我声明了$scope.cat=[]在代码开始时,我得到了相同的错误,我将数据推送到$scope.cat中,但是当我比较parentid和id时,我得到了这个错误。在这种情况下,我在$scope.cat中有两个数据。所以我只想比较这两个数据的id和剩余数据的parentid,记住第二个if语句就在第一个if语句之后。所以有时候将没有范围。类别[i]即使我更改类别[i].parentid我也一样error@user7098427:我已经用优化的代码更新了我的答案,看一看如果我使用你的代码,我的控制台中会出现未定义的问题。你能用plnkri给我看一下吗?我已经用示例plunk更新了我的答案,看一看。我希望这对你有帮助。非常感谢你了解我的建议,添加了你应该替换的内容,我的onl问题是,您可能需要使用过滤器将内容和目录与ID匹配。
    var categories = data;
        for(var i =0;i<=categories.length;i++)
        {
            if(typeof categories[i] != "undefined" && categories[i].parentid ==0)
            {
                $scope.cat.push(categories[i]);
            }
    
        }
        //you will already have you $scope.cat filled then loop through it 
        //checking with the categories array too.
    
        for(var j=0;j<$scope.cat.length;j++)
        {
           for(var k=0; k<categories.length; k++){
             if($scope.cat[j].parentid==categories[k].id)
             {
               $scope.content.push(categories[k]);
             }
           }
        }
    
    var categories = data;
    
    categories = categories
    .filter( (category)=>{
       return (category !== []._);  //removes all undefineds in the array
    })
    .map((category)=>{
      //checks each category from data if is parent_Id is 0 and pushes it to cats array
      //by the end of the operation. the cats array will have some data
      (category.parentId == 0)? ($scope.cats.push(category) , return ) : return;
    })
    var getContent =(cats,categories)=>{
      var content=[];
      for(var j=0;j<cats.length;j++)
      {
         for(var k=0; k<categories.length; k++){
           if(cats[j].parentid==categories[k].id)
           {
             content.push(categories[k]);
           }
         }
      }
      return content;
    }
    $scope.content = getContent($scope.cats,categories);