Search code examples
javascriptarraystypescriptfilterjavascript-objects

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.


Solution

  • 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.