1

Need to convert below request format by using javascript to output format.

Request:

{
  "patientId": "1234",
  "patientName": "Sai",
  "patientFname": "Kumar",
  "patientLname": "Gadi",
  "city": "",
  "zipcode":null,
  "state":" "

}

Need to convert as below format but we need to check object keyvalue of the element should not be null or " "(no space) or ""(not empty) then only we need to print the object name and its values as below format:

Output:

[
 {
  "propertyName": "patientId",
  "propertyValue": "1234"
 },
 {
   "propertyName": "patientName",
   "propertyValue": "Sai"
 },
 {
  "propertyName": "patientFname",
  "propertyValue": "Kumar"
  },
  {
   "propertyName": "patientLname",
    "propertyValue": "Gadi"
   }
]

Thanks in advance.

4 Answers 4

3

Use map and filter on Object.entries:

const data = {
  "patientId": "1234",
  "patientName": "Sai",
  "patientFname": "Kumar",
  "patientLname": "Gadi",
  "city": "",
  "zipcode": null,
  "state": " "
};

const newData = Object.entries(data).filter(([, v]) => ![undefined, null, ""].includes(typeof v == "string" ? v.trim() : v)).map(([key, value]) => ({
  propertyName: key, 
  propertyValue: value
}));

console.log(newData);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Sign up to request clarification or add additional context in comments.

6 Comments

This is nice. It's probably not an issue in this case, but might be in others — this will filter out the number 0.
Whoops @MarkMeyer - I will fix that straight away.
@MarkMeyer I have fixed that issue, any better?
Oh yes @Shidersz that would be easier - I will do that.
Yeah, looks good to me. Not sure why someone filter the need to downvote.
|
0

Here is one simle way to do it:

const obj = {
  "patientId": "1234",
  "patientName": "Sai",
  "patientFname": "Kumar",
  "patientLname": "Gadi",
  "city": "",
  "zipcode":null,
  "state":" "
};
const newArr = [];

for (let key in obj) {
  if (obj[key] && obj[key].trim()) {
    newArr.push({
      propertyName: key,
      propertyValue: obj[key]
    });
  }
}

console.log(newArr);

First, you iterate through the enumerable properties of the object. Within each iteration, you check if the value is not null or empty spaces. If there is a proper value, it will push the new object to the resulting array.

3 Comments

Hi, Thanks a lot for the solution.But the if condition is not getting executed when we are sending "state":" "(with space).
Hey, just to get you right, am I supposed to return the object into the array if there are only spaces?
That is because obj[key].trim() removes spaces from state: '" ", cauing it to be state: "". Therefore, that if condition will check for that too.
0

Array.reduce would be appropriate here. This way you do not have to call Array functions consecutively, just to loop through your array multiple times (ie: Array.map()+Array.filter()).

let obj = {
  "patientId": "1234",
  "patientName": "Sai",
  "patientFname": "Kumar",
  "patientLname": "Gadi",
  "city": "",
  "zipcode": null,
  "state": " "
};

let res = Object.entries(obj).reduce((acc, [key, value]) => {
  if (![undefined, null, ''].includes(typeof value === 'string' ? value.trim() : '')) {
    acc.push({
      propertyName: key,
      propertyValue: value
    });
  }
  return acc;
}, []);

console.log(res);

Comments

-2

You can use Object.entries and reduce

let obj = {
  "patientId": "1234",
  "patientName": "Sai",
  "patientFname": "Kumar",
  "patientLname": "Gadi",
  "city": "",
  "zipcode":null,
  "state":" "
}

let op = Object.entries(obj).reduce((op,[key,value])=>{
  if((value||'').trim()){
    op.push({
    'propertyName' : key,
    'propertyValue': value
   })
  }
  return op
},[])

console.log(op)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.