I have a class component and its state contains a filterItems[] blank array. I need to populate the values inside the filterItems array
So, I have a data coming from API which looks something like this:
0:
emp_code: "a001"
company_code: "company_a"
keys: ["id", "post" ]
dtypes: ["str", "int"]
1:
emp_code: "b001"
company_code: "company_b"
keys: ["sal", "name" ]
dtypes: ["int", "str"]
//so on for other companies and employees
So, like this I have a few employees. Now I have to loop through this api data and extract the information and store it into another array called "filters" and pass it to a function.
My filter array should look something like this:
filters: [
{
key_name: "id",
company_code: "company_a",
input_type: "text",
dtype: "str"
},
{
key_name: "post",
company_code: "company_a",
input_type: "text",
dtype: "int"
},
//... keys for other company
]
essentially, I need to do the following in react
for each emp_code
for each company_code
for each key:
keys[i]== filters.key_name
company_code== filters.company_code
//...so on
The catch here is that, each company has a set of keys so if the employee belongs to the same company then I dont need to add again to the filter array. So, I basically need to check unique company_code and add the different types of keys a company has to the filters array.
getDerivedStateFromPropsmight be an appropriate place to put your mapping