0

I'm trying to filter the object based on the array values given below. I know how to filter object through the key but in this scenario I need to filter it by values.

This is my code:

let _ = [{
        "form_id": "659d796d-aa78-4e74-af68-5ef037bb215b",
        "form_name": "Investigation Forms",
        "form_type": null,
        "created_at": "",
        "updated_at": ""
    },
    {
        "form_id": "9991f556-fc7f-4400-a491-062b12a0bb03",
        "form_name": "Other Investigation Forms",
        "form_type": {
            "form_type_name": "Assessment",
            "form_type_id": "5f4419c1-9351-4338-b894-84a000328cc4"
        },
        "created_at": "",
        "updated_at": ""
    },
    {
        "form_id": "9991f556-fc7f-4400-a491-062b12a0bb03",
        "form_name": "Other/General Investigation Forms",
        "form_type": {
            "form_type_name": "Report",
            "form_type_id": "594ba131-b110-4ec5-855a-9de8440fbc7b"
        },
        "created_at": "",
        "updated_at": ""
    }
]

//-------------------------------------------------------------------------------------------------

const values = [0, "5f4419c1-9351-4338-b894-84a000328cc4", "594ba131-b110-4ec5-855a-9de8440fbc7b"]

plz ignore my below code it is work attempt . 

const output = _.map(item =>
    values.reduce((val, key) => ({
        ...val,
        [key]: item[key]
    }), {})
);
console.log(output);


my expected output :
const values = ["5f4419c1-9351-4338-b894-84a000328cc4"]

output = [{
 "form_id": "9991f556-fc7f-4400-a491-062b12a0bb03",
        "form_name": "Other Investigation Forms",
        "form_type": {
            "form_type_name": "Assessment",
            "form_type_id": "5f4419c1-9351-4338-b894-84a000328cc4"
        },
        "created_at": "",
        "updated_at": ""
}]

The form_type has form_type_id, this is the value I need to be filter by.

8
  • You want to filter based on the value of form_type. form_type_id ? Commented Feb 8, 2021 at 17:09
  • @code yes brother i need like that Commented Feb 8, 2021 at 17:14
  • @code yes i need like thatt Commented Feb 8, 2021 at 17:17
  • What do you want to use as the logic for filtering? If the form_type_id exists, or if it has a specific value? Commented Feb 8, 2021 at 17:19
  • Does this answer your question? Filter nested array in object array by array of values Commented Feb 8, 2021 at 17:19

1 Answer 1

1

If I understood your question correctly then you need

_.filter((item) => values.includes(item.form_type?.form_type_id));

or if optional chaining is not available (as correctly pointed out by Obed Marquez Parlapiano)

_.filter((item) => values.includes(item.form_type && item.form_type.form_type_id));

See test at https://jsfiddle.net/snLqx4g9/

Sign up to request clarification or add additional context in comments.

3 Comments

Note that you may not have access to optional chaining ?.. In that case just use item.form_type && item.form_type.form_type_id
Array.contains are actually deprecated you might not be available in your browser. I have updated the answer and included Obed's comment as well
Added a jsfiddle link where you can play with this. If workink please accept answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.