Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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 js应用程序中的另一个json对象数组为json数组的每个对象填充json对象数组_Javascript_Angularjs_Underscore.js - Fatal编程技术网

Javascript 需要从angular js应用程序中的另一个json对象数组为json数组的每个对象填充json对象数组

Javascript 需要从angular js应用程序中的另一个json对象数组为json数组的每个对象填充json对象数组,javascript,angularjs,underscore.js,Javascript,Angularjs,Underscore.js,在我的Angularjs应用程序中,我有以下json: $scope.listA= [ { "bId":777, "cId":4, "ctNm":"Software" }, { "bId":777, "cId":2, "ctNm":"Hardware" }, { "bId":777, "cId":1, "ctNm":"Malware" } ] 我需要转换这个数组,使数组中的每个json对象都应该包

在我的Angularjs应用程序中,我有以下json:

$scope.listA=

[
  {
    "bId":777,
    "cId":4,
    "ctNm":"Software"
  },
  {
    "bId":777,
    "cId":2,
    "ctNm":"Hardware"
  },
  {
    "bId":777,
    "cId":1,
    "ctNm":"Malware"
  }
] 
我需要转换这个数组,使数组中的每个json对象都应该包含
items
对象数组

为每个
$scope.listA
json对象填充
对象数组的逻辑是去签入
$scope.listB
json对象数组,如果特定的
cId
存在。如果是,将
$scope.listB
的所有json对象保留在
项目下
$scope.listA
中每个对象的数组下

$scope.listB=

 [
  {
    "bId":0,
    "cId":2,
    "clsxId":24,
    "ctNm":"Hardware",
    "clNm":"Out 1"
  },
  {
    "bId":0,
    "cId":1,
    "clsxId":99,
    "ctNm":"Malware",
    "clNm":"Srv"
  },
  {
    "bId":0,
    "cId":2,
    "clsxId":26,
    "ctNm":"Hardware",
    "clNm":"Buss"
  },
  {
    "bId":0,
    "cId":2,
    "clsxId":67,
    "ctNm":"Hardware",
    "clNm":"Pait"
  }
]
因此,最后修改的
$scope.listA
应如下所示:

[
  {
    "bId":777,
    "cId":4,
    "ctNm":"Software",
    "items":[

    ]
  },
  {
    "bId":777,
    "cId":2,
    "ctNm":"Hardware",
    "items":[
      {
        "bId":0,
        "cId":2,
        "clsxId":24,
        "ctNm":"Hardware",
        "clNm":"Out 1"
      },
      {
        "bId":0,
        "cId":2,
        "clsxId":26,
        "ctNm":"Hardware",
        "clNm":"Buss"
      },
      {
        "bId":0,
        "cId":2,
        "clsxId":67,
        "ctNm":"Hardware",
        "clNm":"Pait"
      }
    ]
  },
  {
    "bId":777,
    "cId":1,
    "ctNm":"Malware",
    "items":[
      {
        "bId":0,
        "cId":1,
        "clsxId":99,
        "ctNm":"Malware",
        "clNm":"Srv"
      }
    ]
  }
]

无论是分数不足还是角度js util函数中的解决方案,对我来说都很好。我不喜欢使用for循环,任何其他方式都可以,foreach、map等

for (var a = 0; a < $scope.listA.length; a++) {
    var itemInA = $scope.listA[a];
    itemInA.items = [];
    for (var b = 0; b < $scope.listB.length; b++) {
        var itemInB = $scope.listB[b];
        if (itemInA.cId === itemInB.cId) {
            itemInA.items.push(itemInB);
        }
    }
}

试着这样做:

for (var a = 0; a < $scope.listA.length; a++) {
    var itemInA = $scope.listA[a];
    itemInA.items = [];
    for (var b = 0; b < $scope.listB.length; b++) {
        var itemInB = $scope.listB[b];
        if (itemInA.cId === itemInB.cId) {
            itemInA.items.push(itemInB);
        }
    }
}
你可以试试这个

$scope.listA.forEach(function(aItem){
 aItem.Items =[];
 aItem.Items.addRange($scope.listB.where(function(bItem){return aItem.cId == bItem.cId;}));
});
这里,我使用了一些为javascript数组创建的扩展

Object.defineProperty(Array.prototype, "addRange", {
 value: function (values) {
    for (var i = 0; i < values.length; i++) {
        this.push(values[i]);
    }
    return this;
  }, enumerable: false
});

Object.defineProperty(Array.prototype, "where", {
 value: function (Predicate) {
    var arr = [];
    for (var i = 0; i < this.length; i++) {
        if (Predicate(this[i])) arr.push(this[i]);
    }
    return arr;
  }, enumerable: false
});
Object.defineProperty(Array.prototype,“addRange”{
值:函数(值){
对于(变量i=0;i
对于c#developer来说,像lambda表达式一样使用它有点友好

您可以试试这个

$scope.listA.forEach(function(aItem){
 aItem.Items =[];
 aItem.Items.addRange($scope.listB.where(function(bItem){return aItem.cId == bItem.cId;}));
});
这里,我使用了一些为javascript数组创建的扩展

Object.defineProperty(Array.prototype, "addRange", {
 value: function (values) {
    for (var i = 0; i < values.length; i++) {
        this.push(values[i]);
    }
    return this;
  }, enumerable: false
});

Object.defineProperty(Array.prototype, "where", {
 value: function (Predicate) {
    var arr = [];
    for (var i = 0; i < this.length; i++) {
        if (Predicate(this[i])) arr.push(this[i]);
    }
    return arr;
  }, enumerable: false
});
Object.defineProperty(Array.prototype,“addRange”{
值:函数(值){
对于(变量i=0;i

对于c#developer来说,像lambda表达式一样使用它有点友好

请发布您的一些成果。我尝试了angular。forEach,在我使用listB.map的内部,获取所有匹配的cid,并将所有这些匹配的索引推送到一个数组中,然后迭代lstA,在内部,我再次使用lisB的forEach推送所有匹配的索引,对于索引,我使用count变量来递增。但我觉得一定有一些聪明而简单的方法可以做到这一点。请把你的一些努力发表出来。我试过了。forEach,在我使用listB.map的内部,获取所有匹配的cid,并将所有这些匹配的索引推送到一个数组中,然后迭代lstA,在内部,我再次使用lisB的forEach推送所有匹配的索引,对于索引,我使用count变量来递增。但我觉得一定有一些聪明而简单的方法可以做到这一点。在我读到的一些帖子中,不推荐在angularjs中使用for循环。你能对angular.foreach做同样的操作吗?因为如果你对每个迭代都使用angular,那么我们需要为每个迭代手动计算索引,如果我使用你的解决方案,我对此不太关心的话。看看这个。根据循环的实际内容,angular的foreach实际上比仅使用本机for循环慢84%左右。如果可以轻松地使用本机代码,则无需增加库方法的开销。或者是另一个最近的比较,表明angular的forEach实际上是9种不同循环方法中最慢的。是的,我可以回忆起,当我们需要一次发出多个http请求时,建议不要使用for loop来处理承诺,等等。但是在这里,由于没有承诺、http请求等,我觉得您的解决方案是最好的,而且也是根据您提供的参考。所以我接受了答案。在我读到的一些帖子中,不推荐在angularjs中使用for循环。你能对angular.foreach做同样的操作吗?因为如果你对每个迭代都使用angular,那么我们需要为每个迭代手动计算索引,如果我使用你的解决方案,我对此不太关心的话。看看这个。根据循环的实际内容,angular的foreach实际上比仅使用本机for循环慢84%左右。如果可以轻松地使用本机代码,则无需增加库方法的开销。或者是另一个最近的比较,表明angular的forEach实际上是9种不同循环方法中最慢的。是的,我可以回忆起,当我们需要一次发出多个http请求时,建议不要使用for loop来处理承诺,等等。但是在这里,由于没有承诺、http请求等,我觉得您的解决方案是最好的,而且也是根据您提供的参考。所以我接受了答案。你可以按照@Madasu Ku的建议,在apposed to
forEach
中使用native for loop,也可以按照@Madasu K的建议,在apposed to
forEach
中使用native for loop