Javascript 深入到JSON对象的角度

Javascript 深入到JSON对象的角度,javascript,angularjs,Javascript,Angularjs,我正试图找出最好的方法,将存储为作用域的json对象,过滤/查询它以显示其中的特定数据 例如: $scope.myBook = {"bookName": "Whatever", "pages": [ {"pageid": 1, "pageBody": "html content for the page", "pageTitle": "Page 1"}, {"pageid": 2, "pageBody": "html content for the pag

我正试图找出最好的方法,将存储为作用域的json对象,过滤/查询它以显示其中的特定数据

例如:

$scope.myBook = {"bookName": "Whatever",
    "pages": [
        {"pageid": 1, "pageBody": "html content for the page", "pageTitle": "Page 1"},
        {"pageid": 2, "pageBody": "html content for the page", "pageTitle": "Page 2"},
        {"pageid": 3, "pageBody": "html content for the page", "pageTitle": "Page 3"},
    ]
}
我如何抓取pageid:2的对象

function getPage (id) {
    angular.forEach($scope.myBook.pages, function (page, pageIndex) {
        if (page.pageId == id) {
            console.log("Do something here.");
            return page;
        }
    });
}
否则

$scope.myBook.pages[1];

您可以使用以下方法:

模板:

<div ng-repeat="page in myBook.pages | filter:pageMatch(pageid)">
    {{ page.pageBody }}
</div>

pageid
设置为
filter:pageMatch(pageid)
中所需的值,以显示必要的页面内容。

最后我问了AngularJS IRC,其中一个家伙把这个小玩意儿和我要找的东西放在一起

答案的道具

/* See PLNKR Link Above for the answer */

您的意思是抓取
pageid:2的
pageBody
?使用
for()
查找必要的页面,然后使用
foundPage.pageBody
/* See PLNKR Link Above for the answer */