简体   繁体   中英

Javascript return certain string from body

i am totally new to Javascript so after some help

if (BODY.length && BODY[0]["id"]) {
    result = {
        "EmpID": BODY,
        "Status": "User found",
        "Code": 200
    };
} else {
    result = {
    "Status": "User not found",
    "Code": 404
    };
}

I have the above script which pulls data from a response body and i get a return of

{
    "EmpID": [
        {
            "id": "1EF7C992-CBC1-45AF-83CC-304044E8284B"
        }
    ],
    "Status": "User found",
    "Code": 200
}

the id changes depending on the user but i just need the EmpID to be

"EmpID": 1EF7C992-CBC1-45AF-83CC-304044E8284B or whatever the code is returned without the { } and "id":

eg

{
    "EmpID": 1EF7C992-CBC1-45AF-83CC-304044E8284B
    "Status": "User found",
    "Code": 200
}

Can anyone help?

Thank you

You need to extract the ID from the correct location in your return statement just like you do when you check it exists in your if statement eg

result = {
    "EmpID": BODY[0]["id"],
    "Status": "User found",
    "Code": 200
};

If this is the response you get.

{
    "EmpID": [
        {
            "id": "1EF7C992-CBC1-45AF-83CC-304044E8284B"
        }
    ],
    "Status": "User found",
    "Code": 200
}

then you can return

{
"EmpID": response["EmpID"][0].id,
"Status": "User found",
 "Code": 200
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM