简体   繁体   English

AngularJS过滤器JSON对象{}不是对象数组

[英]AngularJS filter JSON object {} that is not an array of objects

I am trying to apply a filter to a JSON object without much luck. 我试图将过滤器应用于JSON对象,但运气不佳。

The data structure is something like this: 数据结构是这样的:

$scope.jsonObj = {
    someid8979: {
        name: "Bill",
        age: 18
    },
    someid987: {
        name: "Ted",
        age: 17
    }
}

In my HTML, something like this: 在我的HTML中,如下所示:

<input ng-model="search">
<div ng-repeat="(key,val) in jsonObj | filter:search">
    {{key}} - {{val.name}} - {{val.age}}
</div>

In the past, I have always sent out the data as an array of objects, and indeed, if I did that here it would work, but due to the nature of the app, this isn't possible as I need to refer to properties on the object by name (the id) elsewhere in the code. 过去,我一直将数据作为对象数组发送出去,的确,如果我在这里这样做就可以了,但是由于应用程序的性质,这是不可能的,因为我需要引用属性在对象上按名称(ID)在代码中的其他位置。

Is there any way to apply a filter to a JSON object similar to this? 有没有办法像这样将过滤器应用于JSON对象?

I could create a separate array just for filtering purposes and target the JSON object for my other actions, but I'd rather not :) 我可以创建一个单独的数组,仅用于过滤目的,并将JSON对象作为其他操作的目标,但是我不想:)

Edit: I made a duplicate of the object as array and looped over that, which allows filtering. 编辑:我将对象复制为数组,并在该对象上循环播放,从而可以进行过滤。 This solves my problem, but not in the way I had hoped. 这解决了我的问题,但不是我希望的那样。 Could be good enough. 可能足够好。

You could place the comparator function in your controller like in this plunker: 您可以将比较器功能放置在控制器中,如下所示:

http://plnkr.co/edit/n93RjIqLehVymmjjbTQd?p=preview http://plnkr.co/edit/n93RjIqLehVymmjjbTQd?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM