TDG
TDG

Reputation: 1302

Get value from Request URL response data

Requested URL sent - https://www.example.com/detail.guest.html?ppc=FDE466920006DCEFA697BF982FC9C87C5B257ECB2230CBF4D6D6CA740C7B894D5795F70DED928ED3B00C1F3F77DF974DFD73882DEBDD7EC063B37DEB24CF655528FD911109C57961AE314C612772AADFD2E193D572E6F6C8E249A6DAA

Get below response data correctly as expected by 3rd party.

BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2

enter image description here


I want to extract "BookersID", "BookersTitle", "BookersFirstName", "BookersLastName" separately and display this value in input field.

JS:

var bookerID = data[0].BookersID;
var bookerTitle = data[0].BookersTitle;
var bookerFname = data[0].BookersFirstName;
var bookerLname = data[0].BookersLastName;
console.log("BookersID", bookerID);
console.log("BookersTitle", bookerTitle);

But getting error in display value.

Please let me know how to get the value in console log?

Thanks

Upvotes: 0

Views: 1716

Answers (4)

Nick Parsons
Nick Parsons

Reputation: 50684

You could use .reduce() and .split() to create your string into an object, which can then have its properties accessed

const data = "BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2";

const dataObj = data.split(';').reduce((acc, kvp) =>
  ({
    ...acc,
    ...(([key, value]) => ({[key]: value}))(kvp.split('='))
  }), {});

console.log(dataObj);
// access properties:
console.log(dataObj.BookersID);

Upvotes: 0

Patryk Uszyński
Patryk Uszyński

Reputation: 1119

First you need to get data from your xhr request. To do that you need to add callback function. (More info in jQuery.get() documentation)

$.get( endpoint, function( data ) { // add callback to handle response
 // ... parse data here
});

As I understand you need to parse data. It could be done by using String.prototype.split method and simple mapping.

console.log(data) // BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2
var parsed = data.split(';').map(part => ({ name: part.split('=')[0], value: part.split('=')[1] }));

console.log(parsed);

Output:

[
   {name: "BookersID", value: "250100000002"},
   {name: "BookersTitle", value: "Mr"},
   {name: "BookersFirstName", value: "test1"},
   {name: "BookersLastName", value: "test2"}
]

If you want to get data as an object:

var parsedObject = parsed.reduce(
    (obj, item) => Object.assign(obj, {[item.name]: item.value}) ,{});
// {BookersID: "250100000002", BookersTitle: "Mr", BookersFirstName: "test1", BookersLastName: "test2"}

Upvotes: 1

mukund
mukund

Reputation: 2423

var str = 'BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2';
var data = {};
var parsed = str.split(';').map(part => { let x = part.split("="); data[x[0]] = x[1]; console.log(x) });
console.log(data)

Output:

{BookersID: "250100000002", BookersTitle: "Mr", BookersFirstName: "test1", BookersLastName: "test2"}

Upvotes: 0

Ashok Vishwakarma
Ashok Vishwakarma

Reputation: 689

If you getting the same response you need to write a utility function to convert the same into an object

function _convert(responseString) {
  var _obj = {};

  responseString.split(";").forEach(function(pair){
     var _pairArr = pair.split("=");
     _obj[_pairArr[0]] = _pairArr[1];
  });

  reuturn _obj;
}

var responseString = "BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2";
var obj = _convert(responseString);

obj['BookersID']; // 250100000002
// or
obj.BookersID; // 250100000002

Note: This will only work if your response has exactly the same format as you have mentioned.

Upvotes: 0

Related Questions