0

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.

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. How do I achieve this in angularjs or vanilla javascript?

For now I am looping using ng-repeat to create and show data in <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>
kittu
  • 6,662
  • 21
  • 91
  • 185

3 Answers3

1

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

brk
  • 48,835
  • 10
  • 56
  • 78
  • How is resultant object different from object I am passing? Its printing the same object out. I want to create `
  • based on `status` key
  • – kittu Jun 06 '16 at 15:49
  • So if the `status == open` I will create a `
  • object data
  • ` and if the `status==In progress' then another `
  • object data
  • ` – kittu Jun 06 '16 at 15:51
  • Hi, one quick question, from where is the item parameter inside function coming from? We just pass it like that? I am from java background – kittu Jun 06 '16 at 16:01
  • Instead of wd in currentPageWorkOrders do wd.filterArray, after attaching filter array with scope. filterArray will have only those status that you need – brk Jun 06 '16 at 16:03
  • item is just an argument which array.prototype.filter accepts,instead of item you can name it anything you like – brk Jun 06 '16 at 16:04