2

I have a group of filters that is an Reactive Forms Object. I’ve taken the property values of the object and pushed it into an array.

// original filters object {claim_number: null, status: "Approved", patient: null, service_date: null}

let filterArr = []
Object.keys(this.filtersForm.value).forEach(filter => {
    filterArr.push(this.filtersForm.value[filter])
    // filterArr [null, “Approved, null, null]
})

I have a table that is comprised of an array of objects like the following:

"claims":[  
        {  
            "billed_amount":141.78,
            "claim_number": "6596594-0",
            "location":"University Hospital",
            "member_id":"A1234567890",
            "status":{  
                "label":"Approved",
                "value": "Approved"
            }
        },
        {  
            "billed_amount":341.70,
            "claim_number": "2196524-3",
            "location":"Springfield Hospital",
            "member_id":"B1234567890",
            "status":{  
                "label":"Pending",
                "value":"Pending"
            }
        },
        {  
            "billed_amount":111.70,
            "claim_number": "1233514-5",
            "location":"Springfield Hospital",
            "member_id":"C1234567890",
            "status":{  
                "label":"Pending",
                "value":"Pending"
            }
        },
        {
            // ...etc
        }
    ]

I am trying to loop through each row and put the property values in an array, one for each row so I can filter them against filterArr. How can I do that?

My question is similar to this post (From an array of objects, extract value of a property as array ), with the key difference being that I'm trying to create an array per object.

Each object represents a row in a table that I am trying to dynamically filter. So I can't have values from different rows being put into one array.

London804
  • 1,072
  • 1
  • 22
  • 47
  • 2
    What are expected results? – charlietfl Nov 29 '18 at 21:25
  • yeah, would be good to see an example of desired output – Calvin Nunes Nov 29 '18 at 21:31
  • I want the values of each row to be in it's own array. So I can filter each row against the filterArr. Something like claimArr[1]=[141.78, 6596594-0, "University Hospital", etc], claimArr[2]=[341.70, 2196524-3, "Springfield Hospital", etc], – London804 Nov 29 '18 at 21:51
  • I believe I need to use a for loop. for( var i = 0; i < this.rows.length; i++ ) {} I'm trying to filter the table now. – London804 Nov 29 '18 at 21:52

2 Answers2

2

According to your desired result, I think you can use ES6 functions.

const result = yourTable.map(element => Object.values(element));

Using map() function, you go through all elements, and extract from each object its values.

Slawomir Wozniak
  • 489
  • 6
  • 14
  • Yes, this along with the for loop I put in my comments works, thank you. Do you have any idea how to take this a step further and filter the filterArr by the claimArr? – London804 Nov 29 '18 at 22:24
  • Do you mean that that for every element "Approved" in filterArr, we should extract the claimArr element of a corresponding index? Please clarify on simple examples if I'm wrong. – Slawomir Wozniak Nov 30 '18 at 07:28
  • We don't need to extract it just flag it. Also for any values for patient, claim_number, or service_date. I'm building a dynamic filter so I'm trying to match the values from the first array in any of the other arrays. If there is a match I want to flag it and update the view to just show those arrays. – London804 Nov 30 '18 at 16:30
  • Please show some quick examples of the desired result. What do you mean by 'flagging' them? – Slawomir Wozniak Nov 30 '18 at 17:28
  • https://www.tablefilter.com/case-sensitive.html Please look at this URL. My first array is the filters up top. Each row below is in an array. When I type something and hit enter if the text I typed in the filter matches any of the text in the row items they are returned. Is that clear? – London804 Nov 30 '18 at 18:05
  • This did the trick. rowArr.filter(e => { if (filterArr.includes(e)) { filteredResults.push(element); this.rows = filteredResults; } }) – London804 Nov 30 '18 at 18:20
  • @London804 I'm glad you found out the solution. Consider that the `filter` function on each iteration should return a boolean, meaning that the element should land in an array. e.g. `const filteredResults = rowArr.filter(element => filterArr.includes(element));`. – Slawomir Wozniak Nov 30 '18 at 18:25
0

Unsure what you want to include in your output but the below will loop through an array and return an array to the filter function

const output = claimTable["claims"].map((claim) => {
    return claim
}).filter((claim) => {
    return claim.billed_amount > 100
})

The above will loop through the claims and 'convert' to an array. The filter will return that claim for all true conditions (in this case, if the billed amount is greater than 100).

This article goes over this and adds a bit more to it.

Rastalamm
  • 1,712
  • 3
  • 23
  • 32
  • I'm filtering a table. I have a working version, but I need it to be dynamic because I have several tables with different filter options. FilterArr is the values of the filter. ClaimArr is the value of the table rows. I want to filter ClaimArr by FilterArr and return the rows that match. – London804 Nov 29 '18 at 22:22