gpt4 book ai didi

javascript - Angular 中的数组映射

转载 作者:行者123 更新时间:2023-11-30 00:28:15 26 4
gpt4 key购买 nike

我的 JSON 对象

$scope.selectedItems ={
"RECORDS": [
{
"Id": 23040035705987,
"arriveddate": "2015/04/24",
"expirationDate": null,
"replacedDate": null,
"processDate": "2015/04/24"
},
{
"Id": 23070041800654,
"arriveddate": "2015/04/24",
"expirationDate": null,
"replacedDate": null,
"processDate": "2015/04/27"
},
{
"Id": 23040035705984,
"arriveddate": "2015/04/24",
"expirationDate": null,
"replacedDate": null,
"processDate": "2015/04/24"
},
{
"Id": 23040035705983,
"arriveddate": "2015/04/24",
"expirationDate": null,
"replacedDate": null,
"processDate": "2015/04/24"
}
]
}

我在做什么

对于我拥有的每个唯一流程日期,我需要在下面提到的对象中的单独对象中的相应 ID,流程日期 24/04/2015 与以 83、84、87 结尾的 ID 和流程日期 27/04 匹配/2015 匹配以54结尾的id

我期望的 JSON 对象

{
"processDate": [
"2015/04/24",
"2015/04/27"
],
"Id": [
[
23040035705983,
23040035705984,
23040035705987
],
[
23070041800654
]
]
}

我在努力

angular.forEach($scope.selectedItems.RECORDS, function ( item ) { 
$scope.ProcessDate = item.processDate;
if($scope.processDatesSelected.indexOf($scope.ProcessDate) == -1){
$scope.processDatesSelected.push($scope.ProcessDate);
}

if($scope.processDatesSelected.indexOf($scope.ProcessDate) != -1 && $scope.id.indexOf(item.Id) == -1 ){
$scope.id.push(item.Id);
}

});

$scope.changesSelected.push({processDate:$scope.processDatesSelected,Ids:$scope.id});
console.log(JSON.stringify($scope.changesSelected));

问题是 id 没有相应地映射,我已经为它创建了一个 plunker (http://plnkr.co/edit/mkLXdOKayaqyDDDheFeu?p=preview)任何帮助将不胜感激。

最佳答案

尝试这种方式:http://plnkr.co/edit/LS40YJR9X0OAqBTIP2lZ?p=preview

angular.forEach($scope.selectedItems.RECORDS, function ( item ) { 
$scope.ProcessDate = item.processDate;
var i=$scope.processDatesSelected.indexOf($scope.ProcessDate);
if(i == -1){
i=$scope.processDatesSelected.length;
$scope.processDatesSelected.push($scope.ProcessDate);
$scope.id[i]=[];
}

if(i != -1 && $scope.id[i].indexOf(item.Id) == -1 ){
$scope.id[i].push(item.Id);
}

});

关于javascript - Angular 中的数组映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30613047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com