How to find an object's element to return different element from the same object

I'm trying to match the condition from below data to check if headers(data.headers) has accept = application/xml then only it will return the body(data.body) in xml format.

below are the dataset

[ { url: '/a',

    status: '200',

    headers: [ 'content-type=application/json', 'accept=application/json' ],

    body: [ '{ name: Ant Man }' ] },

  { url: '/b',

    status: '400',

    headers: [ 'content-type=application/xml', 'accept=application/xml' ],

    body: '<name>Spiderman</name>' } ]

so I'm writing below code to check all the 4 elements(url, status, headers, body) in a particular format and trying if headers has accept=application/xml then it will return body into xml format.

if (! _.isNil( map.url ) ) map.url = map.url[0]

if (! _.isNil( map.status ) ) map.status = map.status[0]

if (! _.isNil( map.body ) ) {

    if (_.startsWith(map.body, '<')) map.body = map.body.join('\n')

}

if (! _.isNil(map.headers)) {

        ??
    }

Although above code is giving me right answer but not able think of logic behind headers to provide the right condition not by data.body should start with '<' will prove its an xml data. Any help and logical point would be appreciable :-)

1 answer

  • answered 2018-07-11 03:28 psiphi75

    Does this solve your issue?

    if (! _.isNil( map.header ) ) {
    
        if (map.header.includes('accept=application/xml')) map.body = map.body.join('\n')
    
    }