简体   繁体   English

如何为仅 json 对象键而不是 angularjs 中的值制作自定义过滤器

[英]How to make custom filter for a only json objects keys not for values in angularjs

I am new to angularjs and was trying to filter nested json data basically what i am trying to do is trying to make a custom filter for only json objects keys not for values.我是 angularjs 的新手,并试图过滤嵌套的 json 数据,基本上我要做的是尝试为仅 json 对象键而不是值创建自定义过滤器。 i want to show all the keys with value that i will place in a search field.我想显示我将放置在搜索字段中的所有带有值的键。

It is much better idea to iterate through the array instead of object.遍历数组而不是对象是更好的主意。 Transform your object into array and then simply print the values what do you need using the filter:将您的对象转换为数组,然后使用过滤器简单地打印您需要的值:

JavaScript JavaScript

[...]

var arr = [];
for (var i in obj) {
   if (obj.hasOwnProperty(i)) {
      arr.push(obj[i]);
   }
}

[...]

HTML HTML

[...]

<div>
   <input type="text" ng-model="search.a" />
</div>

<div ng-repeat="item in arr | filter:search">
   {{item.a}}
   {{item.b}}
   {{item.c}}
</div>

[...]

EDIT编辑

Related to the picture you posted you need something like this.与您发布的图片相关,您需要这样的东西。

HTML HTML

[...]

<div>
   <input type="text" ng-model="search.id" />
</div>

<div ng-repeat="item in items | filter:search">
   {{item.index}}
   {{item.id}}
</div>

[...]

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

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