Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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 - Fatal编程技术网

Javascript 如何在AngularJS应用程序中计算列表中特定项目的数量

Javascript 如何在AngularJS应用程序中计算列表中特定项目的数量,javascript,angularjs,Javascript,Angularjs,我正在使用以下代码尝试生成一个列表,显示AngularJS控制器中字母列表中每个项目的计数: $scope.letters = ['a','b','c']; $scope.letterdata = ['a','b','a','c','b','a']; $scope.counts = $scope.letters.map(function (x) { $scope.letterdata.filter(function (y) { return (x == y);

我正在使用以下代码尝试生成一个列表,显示AngularJS控制器中
字母
列表中每个项目的计数:

$scope.letters = ['a','b','c'];
$scope.letterdata = ['a','b','a','c','b','a'];

$scope.counts = $scope.letters.map(function (x) {
    $scope.letterdata.filter(function (y) {
        return (x == y);
    }).length;
});

我想将
[3,2,1]
作为输出,但这显示为
[null,null,null]

您缺少
返回
语句,在
映射()中

var字母=['a','b','c'];
var letterdata=['a','b','a','c','b','a'];
变量计数=字母.map(函数(x){
返回字母数据。过滤器(功能(y){
返回(x==y);
}).长度;
});

控制台日志(计数)您在
map()
中缺少
return
语句

var字母=['a','b','c'];
var letterdata=['a','b','a','c','b','a'];
变量计数=字母.map(函数(x){
返回字母数据。过滤器(功能(y){
返回(x==y);
}).长度;
});
控制台日志(计数)
$scope.counts = $scope.letters.map(function (x) {
    return $scope.letterdata.filter(function (y) {
    ^^^^^^
        return (x == y);
    }).length;
});
var list = ['a', 'b', 'c'];
var data = ['a', 'b', 'a', 'c', 'b', 'a'];

_.reduce(list, function(counts, value) {
    counts.push(_.size(data) - _.size(_.without(data, value)));
    return counts;
}, []);