I have this code which process an API request. However, I can’t pull the values from the custom_fields array because the API sometimes sends the data in a different order. Therefore, I’ll need to go through this custom_fields array and extract the needed data by name. I’m having some trouble with this. How can I do this in a neat way that can be dropped into the code below?
this.employees = response.map((e) => ({
id: e.id,
photo: e.photo,
first_name: e.first_name,
name_ru: e.custom_fields[9].value,
last_name: e.last_name,
city: e.city,
city_ru: e.custom_fields[11].value,
country: e.country,
subjects: e.subjects.split(","),
subjects_ru: e.custom_fields[12].value.split(","),
bio: e.bio,
bio_ru: e.custom_fields[8].value,
first: Number(e.custom_fields[10].value)
Here’s an example of an employee’s custom_fields array:
[
{field_id: 17363, name: "Bank details: Bank name", value: "something"},
{field_id: 17940, name: "Bank deatils: Bank country", value: "something"},
{field_id: 17942, name: "Bank details: Sort code", value: "something"},
{field_id: 17943, name: "Bank details: IBAN", value: "something"},
{field_id: 17944, name: "Bank details: SWIFT", value: "something"},
{field_id: 17945, name: "Bank details: Beneficiary name (your name on the banking records)", value: "something"},
{field_id: 17946, name: "Bank details: Account number", value: "something"},
{field_id: 18150, name: "Subject(s)", value: "something"},
{field_id: 18260, name: "Bio RUS", value: "something"},
{field_id: 18261, name: "Name RUS", value: "something"},
{field_id: 18262, name: "Display priority (higher number, higher priority)", value: "something"},
{field_id: 18268, name: "City RUS", value: "something"},
{field_id: 18269, name: "Subjects RUS", value: "something"},
]
This is about as far as I can get, but I don’t know where to go next:
const example = custom_fields.filter(item => item.field_id === 18261);
This outputs:
{field_id: 18261, name: "Name RUS", ...}
What should I do next?
2
Answers
I was able to use this to get a working result:
filter
returns an array. What you want isfind
. You could use this function:And now you can write: