I am getting below response. So, from the response we need to remove “_text” whenever it’s present in any object property. In some cases , i am also receiving key as an array which is causing problem to transform the data with current logic.
[code]
actualResponse = [
{
type: {
_text: “user”,
},
data: {
desc: {
_text: “abbc”,
},
desc1: {
_text: “jhd”,
},
},
data1: {
clients: {
client: [
{
clientName: {
_text: “Test 111”,
},
date: {
_text: “02/02/2020”,
},
},
{
clientName: {
_text: “Test 222”,
},
date: {
_text: “01/02/2020”,
},
},
],
},
},
msg: {},
id: {
_text: “123”,
},
},
{
type: {
_text: “student”,
},
data: {
desc: {
_text: “dgfg”,
},
desc1: {
_text: “jgjg”,
},
},
data1: {
clients: {
client: [
{
clientName: {
_text: “Test 333”,
},
date: {
_text: “02/02/2020”,
},
},
{
clientName: {
_text: “Test 444”,
},
date: {
_text: “01/02/2020”,
},
},
],
},
},
msg: {},
id: {
_text: “134”,
},
},
];
Now, i wanted to remove the ._text from response.
[code]
expectedResponse = [
{
type: “user”,
data: {
desc: “abbc”,
desc1: “jhd”,
},
msg: {},
id: “123”,
data1: {
clients: {
client: [
{
clientName: “Test 111”,
date: “02/02/2020”,
},
{
clientName: “Test 222”,
date: “01/02/2020”,
},
],
},
},
},
{
type: “student”,
data: {
desc: “dgfg”,
desc1: “jgjg”,
},
msg: {},
id: “134”,
data1: {
clients: {
client: [
{
clientName: “Test 333”,
date: “02/02/2020”,
},
{
clientName: “Test 444”,
date: “01/02/2020”,
},
],
},
},
},
];
I use the below logic to convert but it failed to convert data1 property from actual response. Instead it end up in infinite -loop. So i had to modify the below code
[code]
function convObj(ob) {
for (key in ob) {
if (ob[key]._text) {
ob[key] = ob[key]._text;
} else {
if(key === “0”) {
break; // modified logic because key is coming as an array but it failed to transform
}
convObj(ob[key]);
}
}
}
actualResponse.forEach(item => {
convObj(item);
});
console.log(actualResponse);