I need to get the count of an ID existence by comparing it with another json. however in a way i am successful but for few numbers it is getting incorrect count.
var outputProducts = [
{ outputId:'1', inputId: '3', opName: "outMacy" },
{ outputId:'2', inputId: '3', opName: "outMacy2" },
{ outputId:'3', inputId: '3', opName: "outMacy3" },
{ outputId:'4', inputId: '235', opName: "EXPENCE" },
{ outputId:'5', inputId: '235', opName: "opEXPENCE" },
{ outputId:'6', inputId: '235', opName: "opEXPENCE1" },
{ outputId:'8', inputId: '235', opName: "opEXPENCE3" },
{ outputId:'7', inputId: '215', opName: "JCPenny" }
];
this is how my json looks like from which i have to get
temp = [
{ name: "3", shownName: 3 },
{ name: "215", shownName: 1 },
{ name: "235", shownName: 4 }
]
instead it is giving me
temp = [
{ name: "3", shownName: 7 },
{ name: "215", shownName: 1 },
{ name: "235", shownName: 4 }
]
i feel it is looking 3 in the whole json that way it is getting the count of 235 in which it has 3 and showing it as 7 instead of 3
here is my plunker(whole json is not seen because of PUSH error however you can see the generated temp json with the incorrect count of name:"3", shownName:7)
https://plnkr.co/edit/aIdlWUS7UFb91hKC4nqx?p=preview
Here is my code
var outputProducts = [
{ outputId:'1', inputId: '3', opName: "outMacy" },
{ outputId:'2', inputId: '3', opName: "outMacy2" },
{ outputId:'3', inputId: '3', opName: "outMacy3" },
{ outputId:'4', inputId: '235', opName: "EXPENCE" },
{ outputId:'5', inputId: '235', opName: "opEXPENCE" },
{ outputId:'6', inputId: '235', opName: "opEXPENCE1" },
{ outputId:'8', inputId: '235', opName: "opEXPENCE3" },
{ outputId:'7', inputId: '215', opName: "JCPenny" }
];
$scope.outputProducts = outputProducts;
var keysCount, copyOfOutputData, keysCount2, uniqueInputFiles=[], outputFilesCount=[];
keysCount = Object.keys(outputProducts).length;
console.log("$scopes.", $scope.outputProducts);
for (i = 0; i < keysCount; i += 1) {
if (uniqueInputFiles.indexOf(outputProducts[i].inputId) === -1) {
uniqueInputFiles.push(outputProducts[i].inputId);
}
}
console.log("uniqueInputFiles", uniqueInputFiles);
copyOfOutputData = angular.copy(uniqueInputFiles);
keysCount2 = Object.keys(copyOfOutputData).length;
for (i = 0; i < keysCount2; i += 1) {
outputFilesCount = $filter('filter')( outputProducts, { inputId: copyOfOutputData[i] } ).length;
temp = {
name : copyOfOutputData[i],
shownName: outputFilesCount
};
console.log("temp", temp)
$scope.inputFilesWithCount.push(temp);
}
Any help is appreciated