我有一个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?有什么想法吗?
谢谢
答案 0 :(得分:1)
您可以使用原生Array.prototype.filter方法:
var images = response.filter(function(el) {
return /\.(png|jpe?g)$/.test(el.filename);
});
答案 1 :(得分:0)
未经测试的:
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);
}
答案 2 :(得分:0)
$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);
});