gpt4 book ai didi

javascript - 如何在嵌套的 Json 中使用 angularjs 进行过滤

转载 作者:行者123 更新时间:2023-11-29 23:26:25 25 4
gpt4 key购买 nike

我需要在嵌套的 JSON 数组中进行查询,如下所示。我想检查名字是否是 John 以及爱好是否有运动,但没有成功。

如下代码的结果:

[
{"id":1,"name":"John","hobbies":[{"kind":"sport"},{"kind":"music"}]},
{"id":5,"name":"John","hobbies":[{"kind":"swimming"},{"kind":"opera"}]}
]

但它必须是 {"id":1,"name":"John","hobbies":[{"kind":"sport"}{"kind":"music"}]}

function MyCtrl($scope, $filter) {
$scope.items = [
{id:1, name:'John',hobbies:[{kind:"sport"},{kind:"music"}]},
{id:2, name:'Steve',hobbies:[{kind:"opera"},{kind:"theatre"}]},
{id:3, name:'Joey'},
{id:4, name:'Mary'},
{id:5, name:'John',hobbies:[{kind:"swimming"},{kind:"opera"}]}];
$scope.filteredData = $filter('filter')($scope.items, {name:'John',hobbies:[{kind:''}]});
$scope.json=JSON.stringify($scope.filteredData);
// $scope.json2=JSON.stringify($scope.y);
};

最佳答案

1.3.8版本支持这种过滤器

<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script>
(function() {

angular.module("myapp", []).controller('MainCtrl', ['$scope', '$filter', function($scope, $filter) {

$scope.items = [
{id:1, name:'John',hobbies:[{kind:"spor"},{kind:"music"}]},
{id:2, name:'Steve',hobbies:[{kind:"opera"},{kind:"theatre"}]},
{id:3, name:'Joey'},
{id:4, name:'Mary'},
{id:5, name:'John',hobbies:[{kind:"swimming"},{kind:"opera"}]}
];

$scope.filteredData = $filter('filter')($scope.items,
{ name: 'John', hobbies: [ {kind: 'swimming'}]}
);

}]);
}());
</script>
<style></style>
</head>

<body ng-app="myapp" ng-controller="MainCtrl">

{{filteredData}}

</body>

</html>

关于javascript - 如何在嵌套的 Json 中使用 angularjs 进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49094432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com