使用AngularJS仅从JSON响应获取图像

使用AngularJS仅从JSON响应获取图像,json,angularjs,filenames,Json,Angularjs,Filenames,我有一个REST调用,它提供与我传递给该调用的Id相关的所有附件 JSON响应应该是这样的 [ { "id": 1, "filename": "test.txt", "size": 594, "description": "comment", "createdAt": "2014-04-07 12:53:24", "binaryId": 1 }, { "id": 2, "filename": "dummy.png", "size": 496, "description": "comment",

我有一个REST调用,它提供与我传递给该调用的Id相关的所有附件

JSON响应应该是这样的

[
{
"id": 1,
"filename": "test.txt",
"size": 594,
"description": "comment",
"createdAt": "2014-04-07 12:53:24",
"binaryId": 1
},
{
"id": 2,
"filename": "dummy.png",
"size": 496,
"description": "comment",
"createdAt": "2014-04-07 12:53:34",
"binaryId": 2
},
{
"id": 3,
"filename": "else.pdf",
"size": 496,
"description": "comment",
"createdAt": "2014-04-07 12:53:34",
"binaryId": 3
},
{
"id": 4,
"filename": "some.jpg",
"size": 496,
"description": "comment",
"createdAt": "2014-04-07 12:53:34",
"binaryId": 4
}
]
像这样,我会得到所有文件的所有扩展名

所以我在这里需要的是只从json响应中获取图像(.jpg,.png),忽略其他文件,如PDF、文本文件等

我怎么能用AngularJS做这个?有什么想法吗

谢谢

您可以使用本机方法:

未经测试:

var files = [];
angular.forEach($data, $value)
{
    var filename = $value['filename'];
    var length = filename.length;
    var extension = filename.substr(length-3, 3);
    if(extension == "jpg") files.push($value);
}
var files = [];
angular.forEach($data, $value)
{
    var filename = $value['filename'];
    var length = filename.length;
    var extension = filename.substr(length-3, 3);
    if(extension == "jpg") files.push($value);
}
$scope.sampleImageArray = [];
    $scope.imageArray = [
        {
            "id": 1,
            "filename": "test.txt",
            "size": 594,
            "description": "comment",
            "createdAt": "2014-04-07 12:53:24",
            "binaryId": 1
        },
 {
"id": 2,
"filename": "dummy.png",
"size": 496,
"description": "comment",
"createdAt": "2014-04-07 12:53:34",
"binaryId": 2
},
 {
"id": 3,
"filename": "else.pdf",
"size": 496,
"description": "comment",
"createdAt": "2014-04-07 12:53:34",
"binaryId": 3
},
{
"id": 4,
"filename": "some.jpg",
"size": 496,
"description": "comment",
"createdAt": "2014-04-07 12:53:34",
"binaryId": 4
 }
    ];

   angular.forEach($scope.imageArray, function (data) {
            var extension = data.filename.substr((data.filename.lastIndexOf('.') + 1));
            if(extension=="png" || extension=="jpg" || extension=="jpeg")
                $scope.sampleImageArray.push(data);
        });