London804
London804

Reputation: 1116

From an array of objects, extract value of properties for each object and put in a different array

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.

Upvotes: 2

Views: 2539

Answers (2)

Rastalamm
Rastalamm

Reputation: 1782

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.

Upvotes: 0

Slawomir Wozniak
Slawomir Wozniak

Reputation: 518

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.

Upvotes: 2

Related Questions