简体   繁体   English

如何根据同一对象内给定的键值过滤对象

[英]How to filter objects based on value of key given inside same object

I have list of objects where I have to filter the objects based on value of a key present inside same object and create <div> tag with content. 我有一些对象列表,在这些列表中,我必须根据同一对象内存在的键值来过滤对象,并创建具有内容的<div>标签。

List of objects: 对象列表:

{
    "statusCode" : 200,
    "statusMessage" : "OK",
    "result" : [
    {
            "owner" : {
                "id" : "3f32ce2f-4300-439b-84bc-92ad46fbccf7"
            },
            "name" : "Test 12345",
            "description" : "Test 12345",
            "created_at" : 1465222538,
            "active" : "true",
            "id" : "2ade9236-c382-400c-9c0b-94e96db9b2aa",
            "status" : "OPEN"
        }, {
            "owner" : {
                "id" : "3f32ce2f-4300-439b-84bc-92ad46fbccf7"
            },
            "name" : "sample2",
            "description" : "sample2",
            "created_at" : 1465117865,
            "active" : "true",
            "id" : "8bf206f9-d3e0-43f4-ba3f-f71c88db9b0e",
            "status" : "IN PROGRESS"
        }, 
    ]
}

I want to filter data based on "status" : "IN PROGRESS" or "status" : "OPEN" and show the data inside <li> tag but the thing is identifier(key) is present inside the same object. 我想根据"status" : "IN PROGRESS""status" : "OPEN"来过滤数据,并在<li>标记内显示数据,但同一对象内却存在identifier(key)。 How do I achieve this in angularjs or vanilla javascript? 如何在angularjs或vanilla javascript中实现此目的?

For now I am looping using ng-repeat to create and show data in <li> : 现在,我循环使用ng-repeat在<li>创建和显示数据:

<div>
    <ul>
        <li ng-repeat="wd in currentPageWorkOrders>
            <a ng-click="viewProject(wd)"  href="">
                <h4>{{wd.name}}</h4>
            </a>
            <p>Publisher Name</p>
        </li>
    </ul>
</div>

You can use filter method to filter out the required data 您可以使用过滤器方法过滤掉所需的数据

var _getResult = myArray[0].result;
var filterArray = _getResult.filter(function(item){
 return item.status ==="OPEN" || item.status === "IN PROGRESS"

})
console.log(filterArray)

See this jsfiddle 看到这个jsfiddle

You can use ng-if to filter the items.codepen url for reference http://codepen.io/nagasai/pen/MeaawO 您可以使用ng-if过滤项目。codepen网址以供参考http://codepen.io/nagasai/pen/MeaawO

  <li ng-repeat="wd in currentPageWorkOrders.result" ng-if="wd.status == 'OPEN'">
                          <a ng-click="viewProject(wd)"  href="">
                              <h4  >{{wd}}</h4>

                          </a>

                      </li>

Hope this is helpful for you 希望这对您有帮助

You can use filters by Angular in a shorter way. 您可以按较短的方式使用Angular过滤器。 eg You can filter in the ng-repeat. 例如,您可以过滤ng-repeat。

So, your ng-repeat will be something like this: 因此,您的ng-repeat将如下所示:

<li ng-repeat="wd in elements | filter:search">
  <a ng-click=" viewProject(wd) " href=" ">
    <h4>{{wd.name}}</h4
  </a>
  <p>Status: {{wd.status}}</p>
</li>

Where the search is a new element where you set the fiters elements. 搜索是在其中设置fiters元素的新元素。 In my case I use: 就我而言,我使用:

<span class="btn btn-default" ng-click="search.status=''">No Filters</span>
<span class="btn btn-primary" ng-click="search.status='OPEN'">Show Open</span>
<span class="btn btn-success" ng-click="search.status='IN PROGRESS'">Show In Progress</span>

You can check how it works, in the Plunkr: https://plnkr.co/edit/5V3Rg0lggjnPVFu3srsG?p=preview 您可以在Plunkr中检查其工作方式: https ://plnkr.co/edit/5V3Rg0lggjnPVFu3srsG ? p = preview

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

相关问题 使用纯JavaScript,给定对象集合和过滤器对象,返回集合中具有与过滤器对象相同的键/值对的对象 - Using pure JavaScript, given a collection of objects and a filter object, return objects in collection with same key/value pair as the filter object 从数组中过滤在给定键处具有相同值的对象 - filter objects from array that have the same value at given key 获取对象数组内对象的给定键的值 - Get value of given key of object inside array of objects 如何过滤对象数组并检查数组内是否有多个 object 在 Javascript 中具有相同的属性值? - How to filter array of objects and check if more than one object inside the array has the same property value in Javascript? 基于给定键将对象数组转换为对象 - convert an array of objects to an object based on a given key 如何根据一个对象内部的字段过滤对象数组? - How to Filter array of objects based on a field inside one object? 如何根据值子字符串过滤键值对象 - how to filter the key value object based on value substring 如何根据匹配值过滤 object 中的键值? - How do i filter a value of key in an object based on matching value? 根据键过滤 Object 并获取该键的值 - FIlter an Object based on a key and get the value for that key 基于键值对过滤对象 - Filter objects based on key value pair
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM