-1

I am getting data from backend in the below format:

[{
    patientId=2501, 
    uniqueId=PID140, 
    firstName=bairava, 
    middleName=, 
    lastName=surya, 
    emailAddress=**1717259005##@noemailaddress.com, 
    birthday=2016-01-07 00:00:00.0, 
    portraitId=0, 
    race=, 
    companyId=10253, 
    ssn=123-54-7678, 
    ethnicity=0, 
    bloodGroup=0, 
    gender=1, 
    patientLanguageId=,         
    folderId=53501, 
    activationReason=, 
    deactivationReason=, 
    patientStatus=Active, 
    activationRequest=false, 
    cashPayment=true
}]

How can I iterate and get each details individually?

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135

1 Answers1

0

It's quite hard to explain. Just read the code and try to understand.

The way is to convert this string to a JavaScript structure object.

// The output html from the server
var msglis='[{ patientId=2501, uniqueId=PID140, firstName=bairava, middleName=, lastName=surya, emailAddress=**1717259005##@noemailaddress.com, birthday=2016-01-07 00:00:00.0, portraitId=0, race=, companyId=10253, ssn=123-54-7678, ethnicity=0, bloodGroup=0, gender=1, patientLanguageId=, folderId=53501, activationReason=, deactivationReason=, patientStatus=Active, activationRequest=false, cashPayment=true }, { patientId=2506, uniqueId=PID140, firstName=bairava, middleName=, lastName=surya, emailAddress=**1717259005##@noemailaddress.com, birthday=2016-01-07 00:00:00.0, portraitId=0, race=, companyId=10253, ssn=123-54-7678, ethnicity=0, bloodGroup=0, gender=1, patientLanguageId=, folderId=53501, activationReason=, deactivationReason=, patientStatus=Active, activationRequest=false, cashPayment=true }]';

var arr = msglis.replace(/=/g, ':').replace(/:.*?[,}]/g, function(a, b, c) {
  console.log(c);
  var comma = a.indexOf(',') > -1;
  var val = a.replace(':', '').replace(',', '');
  
  if (/^\d+$/g.test(val)) {
    return a;
  }
  else {
    return ':"' + val + '"' + (comma ? ',' : '}');
  }
});


var obj = eval(arr);
document.body.innerHTML = JSON.stringify(obj);

You can see the regex in action here

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • thanks alot for your reply doesnot work if it has more than one objects.... – user3235308 Jan 20 '16 at 12:24
  • can you provide me code that works for multiple objects.. – user3235308 Jan 20 '16 at 12:43
  • thanks alot man!!!!!!!!!!! – user3235308 Jan 20 '16 at 12:56
  • My pleasure ;) That's was challenging ;) If it does do the job, please accept this answer so it will help to other people. – Mosh Feu Jan 20 '16 at 12:58
  • when am passing it to a function --'' it shows error as SyntaxError: missing ] after element list so please help it out ASAP. – user3235308 Jan 20 '16 at 13:26
  • `obj` is the object from the snippet? I guess that you add this input dynamically with JavaScript. If so, you can't do this like this. You need to convert the `object` to `string` first using `JSON.stringify` like this: `` – Mosh Feu Jan 20 '16 at 13:31
  • when i do this i get SyntaxError: invalid property id..so my goal i need to pass the msglis variable to the function and i wil use your code to convert it to the string object so help me out to send inside function. – user3235308 Jan 20 '16 at 13:34
  • Are you generate this button with JavaScript? – Mosh Feu Jan 20 '16 at 13:45