skip to Main Content

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


  1. Chosen as BEST ANSWER

    I was able to use this to get a working result:

    custom_fields.filter(item => item.field_id === 18261).map(item => item.value);
    

  2. filter returns an array. What you want is find. You could use this function:

    const getField = (fields, id) => fields.find(({field_id}) => field_id = id)?.value;
    

    And now you can write:

    this.employees = response.map((e) => ({
        id: e.id,
        photo: e.photo,
        first_name: e.first_name,
        name_ru: getField(e.custom_fields, 18261),
        last_name: e.last_name,
        city: e.city,
        city_ru: getField(e.custom_fields, 18268),
        country: e.country,
        subjects: e.subjects.split(","),
        subjects_ru: getField(e.custom_fields, 18269)?.split(","),
        bio: e.bio,
        bio_ru: getField(e.custom_fields, 18260),
        first: Number(getField(e.custom_fields, 18262))
    }));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search