skip to Main Content

I’m getting the following data returned from an API:

{
    "statusCode": 200,
    "successful": true,
    "userPreferences": [
        {
            "category": "Alerts",
            "preference": {
                "ContentViolations": "true",
                "UserCommentsYou": "true",
                "UserMessagesYou": "true"
            }
        },
        {
            "category": "Display",
            "preference": {
                "DateFormat": "mm-dd-yyyy",
                "TimeFormat": "12-hour"
            }
        },
        {
            "category": "EmailList",
            "preference": {
                "ReceiveDailyDigest": "true",
                "ReceiveDealsOffers": "true"
            }
        },
        {
            "category": "Privacy",
            "preference": {
                "OtherUsersMessageYou": "true",
                "OtherUsersViewProfile": "true"
            }
        },
        {
            "category": "Units",
            "preference": {
                "Measurement": "inches",
                "Weight": "pounds"
            }
        }
    ]
}

I need to extract a preference value from a key value pair. Here is what I have:

const result = userPreferences?.filter(x => x.category === "Alerts" && x.preference["UserMessagesYou"]).map(x => x.preference.value);

It’s returning string[] | undefined but I would like it to retutn string.

In the example above I would like result to have the value "true".

3

Answers


  1. The way I see it, I assume you want to extract one UserMessagesYou value from userPreferences array which has category Alerts right? If so, this should do it:

    const data = {
        "statusCode": 200,
        "successful": true,
        "userPreferences": [
            {
                "category": "Alerts",
                "preference": {
                    "ContentViolations": "true",
                    "UserCommentsYou": "true",
                    "UserMessagesYou": "true"
                }
            },
            {
                "category": "Display",
                "preference": {
                    "DateFormat": "mm-dd-yyyy",
                    "TimeFormat": "12-hour"
                }
            },
            {
                "category": "EmailList",
                "preference": {
                    "ReceiveDailyDigest": "true",
                    "ReceiveDealsOffers": "true"
                }
            },
            {
                "category": "Privacy",
                "preference": {
                    "OtherUsersMessageYou": "true",
                    "OtherUsersViewProfile": "true"
                }
            },
            {
                "category": "Units",
                "preference": {
                    "Measurement": "inches",
                    "Weight": "pounds"
                }
            }
        ]
    }
    
    const result = data.userPreferences.find(userPreference => userPreference.category === "Alerts").preference.UserMessagesYou
    
    console.log(result)
    Login or Signup to reply.
  2. The prop preference does not have a props named value ,so it’s undefined

    const result = userPreferences?.filter(x => x.category === "Alerts" && x.preference["UserMessagesYou"]).map(x => x.preference.UserMessagesYou).join(' ');
    Login or Signup to reply.
  3. it returns undefined because the property value does not exist within the preference object.

    (It’s in the last operation that you do within the map function: .map(x => x.preference.value))

    According to your validation, the properties you have available are:

    • ContentViolations
    • UserCommentsYou
    • UserMessagesYou

    Then you can do something like this:

    const response = {
        "statusCode": 200,
        "successful": true,
        "userPreferences": [
            {
                "category": "Alerts",
                "preference": {
                    "ContentViolations": "true",
                    "UserCommentsYou": "true",
                    "UserMessagesYou": "true"
                }
            },
            {
                "category": "Display",
                "preference": {
                    "DateFormat": "mm-dd-yyyy",
                    "TimeFormat": "12-hour"
                }
            },
            {
                "category": "EmailList",
                "preference": {
                    "ReceiveDailyDigest": "true",
                    "ReceiveDealsOffers": "true"
                }
            },
            {
                "category": "Privacy",
                "preference": {
                    "OtherUsersMessageYou": "true",
                    "OtherUsersViewProfile": "true"
                }
            },
            {
                "category": "Units",
                "preference": {
                    "Measurement": "inches",
                    "Weight": "pounds"
                }
            }
        ]
    }
    
    const result = response.userPreferences?.filter(x => x.category === "Alerts" && x.preference["UserMessagesYou"]).map(x => x.preference.UserMessagesYou);
    console.log("results ====>")
    console.log(result)
    

    Property UserMessagesYou is accessed on the map:

    .map(x => x.preference.UserMessagesYou)
    

    I hope this answer has helped you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search