我想匹配角度foreach中的特定id

时间:2015-04-12 06:50:54

标签: javascript jquery arrays angularjs

我正在尝试匹配id,我需要获得具体的描述我该如何才能得到这个?

我该如何解决这个问题?

$scope.getAlbumList = '[{"album_desc":"Test1","id":1,"album_title":"Test","ImageName":"Desert.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Desert.jpg","large_pic_loc":"Desert.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]},{"album_desc":"Test1","id":2,"album_title":"Test1","ImageName":"Koala.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Koala.jpg","large_pic_loc":"Koala.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]},{"album_desc":"Album Title","id":3,"album_title":"Album Title","ImageName":"Tulips.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Tulips.jpg","large_pic_loc":"Tulips.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]},{"album_desc":"fsdfdsfds fsdfds fds fdsfdsfdsfsdfdsfbdsfkdskjfh dskjfh skjdfhdskjhfk sjdhfdskjhfksjdhfkjdsfh sf dfsjfnkjdshf dsjhf ksksfhkjfhds fdshfhkjdsfhdskjhfkj dshf dskjfh skjdhfskjdhfkjdsfhdskjfhkjdsfhsd fkjdshfkjsdhfkj hjshdjfhdskjfhs fdsjhf dskjhfdskjfh dskjfhdskjfhsdkjfhdskjfsdk jdshfkjdsh kjfdshkjfshkjdfshkj fdshkjdskjsh kjds hfkjsd kdshfkjdshfkjsdhfkjdshfkjdshfkjsdkjf fkjdsh fkjshdfkjdshf dskjf","id":4,"album_title":"sdfds","ImageName":"Lighthouse.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Lighthouse.jpg","large_pic_loc":"Lighthouse.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]}]';
$scope.getalbumarray = 'http://localhost/learn/comment/#/album_details/1';

        angular.forEach($scope.getAlbumList, function(item) {
        $scope.albumDescription = item.id;      
        //console.log(item.id);
        //console.log($scope.getalbumarray[$scope.getalbumarray.length - 1]);
        if(item.id == $scope.getalbumarray[$scope.getalbumarray.length - 1]){
            $scope.albumDescription = item.album_desc;
            //console.log('1');
        }else{
            //console.log('2');
            $scope.albumDescription = item.album_desc;
        }
         });

         console.log($scope.albumDescription);
        $scope.showDescrption = $scope.albumDescription;

4 个答案:

答案 0 :(得分:1)

$scope.getAlbumList 
自添加

后,

已成为字符串

$scope.getAlbumList = '[{"album_desc":"Test1", .... ,"pic_id":"d5bd"}]}]';

删除引号。它看起来应该是这样的:

$scope.getAlbumList = [{"album_desc":"Test1", .... ,"pic_id":"d5bd"}]}];

答案 1 :(得分:1)

试试这个



$scope.getAlbumList = '[{"album_desc":"Test1","id":1,"album_title":"Test","ImageName":"Desert.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Desert.jpg","large_pic_loc":"Desert.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]},{"album_desc":"Test1","id":2,"album_title":"Test1","ImageName":"Koala.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Koala.jpg","large_pic_loc":"Koala.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]},{"album_desc":"Album Title","id":3,"album_title":"Album Title","ImageName":"Tulips.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Tulips.jpg","large_pic_loc":"Tulips.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]},{"album_desc":"fsdfdsfds fsdfds fds fdsfdsfdsfsdfdsfbdsfkdskjfh dskjfh skjdfhdskjhfk sjdhfdskjhfksjdhfkjdsfh sf dfsjfnkjdshf dsjhf ksksfhkjfhds fdshfhkjdsfhdskjhfkj dshf dskjfh skjdhfskjdhfkjdsfhdskjfhkjdsfhsd fkjdshfkjsdhfkj hjshdjfhdskjfhs fdsjhf dskjhfdskjfh dskjfhdskjfhsdkjfhdskjfsdk jdshfkjdsh kjfdshkjfshkjdfshkj fdshkjdskjsh kjds hfkjsd kdshfkjdshfkjsdhfkjdshfkjdshfkjsdkjf fkjdsh fkjshdfkjdshf dskjf","id":4,"album_title":"sdfds","ImageName":"Lighthouse.jpg","album_pics":[{"media_type":"image/jpeg","last_modified_date":1247549551000,"thumnail_pic_loc":"Lighthouse.jpg","large_pic_loc":"Lighthouse.jpg","filter_type":"image/jpeg","pic_id":"d5bd"}]}]';
$scope.getalbumarray = 'http://localhost/learn/comment/#/album_details/1';

        angular.forEach(JSON.parse($scope.getAlbumList), function(item) {
        $scope.albumDescription = item.id;      
        //console.log(item.id);
        //console.log($scope.getalbumarray[$scope.getalbumarray.length - 1]);
        if(item.id == $scope.getalbumarray[$scope.getalbumarray.length - 1]){
            $scope.albumDescription = item.album_desc;
            //console.log('1');
        }else{
            //console.log('2');
            $scope.albumDescription = item.album_desc;
        }
         });

         console.log($scope.albumDescription);
        $scope.showDescrption = $scope.albumDescription;




答案 2 :(得分:1)

您没有从URL字符串中提取相册ID,现在您将获得id的最后一个字符,该字符将突破其中包含多于2个字符的ID。

试试这个

$scope.getalbumarray = 'http://localhost/learn/comment/#/album_details/1';

var albumId = $scope.getalbumarray.split('/').pop();
var albums = $scope.getAlbumList.filter(function(item) {
    return item.id === albumId;
})[0];

之后albums将首先匹配相册。我假设您只需要其中一个(如果没有找到任何内容,则只会有一个或undefined)。

答案 3 :(得分:1)

使用此功能可以帮助您

  $scope.getalbumarray = window.location.href.split('/');

        $scope.albumDescr = '';
        var albumId = $scope.getalbumarray[$scope.getalbumarray.length - 1];
        var albums = $scope.getAlbumList.filter(function(item) {

            if(item.id == albumId){             
                $scope.albumDescr += item.album_desc;
            }           
            //return $scope.albumDescription = item.album_desc;;
        })[0];

        $scope.albumDescription = $scope.albumDescr;
        console.log('$scope.albumDescription');