0

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

2 Answers 2

1

Use reduce and map:

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"    }
];
function inc(t,x) {
  var i = x.inputId;
  t[i]? t[i].count++:(t[i] = {count: 1})
 return t;
 }
var x = outputProducts.reduce((t,x)=> inc(t,x),{});
var y = Object.keys(x).map(v=>({name: v, shownName: x[v].count}))
console.log(y)

Sign up to request clarification or add additional context in comments.

Comments

0

The filter filter is not an exact comparison, it is a pattern when matched using an object. So when you pass it...

{
    inputId: '3'
}

... you are actually asking to find any value that has 3 within the property inputId. For your example, something like this would be more friendly:

$scope.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"    }
];

var counts = {};

angular.forEach($scope.outputProducts, function (product) {
    if (typeof counts[product.inputId] === 'undefined') {
        counts[product.inputId] = 0;
    }

    counts[product.inputId] = counts[product.inputId] + 1;
});

$scope.inputFilesWithCount = [];
angular.forEach(counts, function (total, inputId) {
    $scope.inputFilesWithCount.push({
        name: inputId,
        shownName: total
    });
});

This will generate the values that you are hoping for (in a cleaner, more efficient way) as all you are really doing is updating a count.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.