1

I have the following JsonObject

let jsonObject = {
  "augmentedReality": {
    "enabled": false,
    "augmentedRealitySettings" : [
      {
        "assetId": 7
      }
    ]
  }
}

I am have written a recursive function that looks the following

 isAssetId(jsonObject: any) {

for (let key in jsonObject) {

  if (typeof jsonObject[key] === "object") {

    jsonObject[key] =  this.isAssetId(jsonObject[key]);

  } else {
   
    if(key=='assetId'){
      jsonObject[key]=3;

    }} }

return jsonObject;

}

My goal is to change the assetId wherever it exists in the jsonObject. This JSON is just an example, while assetId could be far in the deeper. The problem with the code is that when it's successfully executed it returns the following JSON object I call the function with the following:

jsonObject=  isAssetId(jsonObject);

console.log(jsonObject);

and I get the following results.

  {
  augmentedReality: { enabled: false, augmentedRealitySettings: [ 
  [Object] ] }
   }
 

The Object should show the data it has not the object. I cannot figure out what seems to be the problem. Any help would be appreciated?

UPDATE: I wrote the code into the following site here Weirdly it's working fine here, but it does not work on my typescript on NestJs? Now what is the reason?

ahmed ali
  • 13
  • 5

2 Answers2

0

The problem is "this" keyword in the function. For more information please check post.

let jsonObject = {
  "augmentedReality": {
    "enabled": false,
    "augmentedRealitySettings" : [
      {
        "assetId": 7
      }
    ]
  }
}


var json = isAssetId(jsonObject);

console.log(JSON.stringify(json));

isAssetId(jsonObject) {

for (let key in jsonObject) {

  if (typeof jsonObject[key] === "object") {

    jsonObject[key] =  isAssetId(jsonObject[key]);

  } else {
   
    if(key=='assetId'){
      jsonObject[key]=3;

    }} }

return jsonObject;

}
Shafqat Jamil Khan
  • 1,039
  • 1
  • 9
  • 17
0

There is something wrong with the logic of your code. At one point you are looping through an array like an object. You could do something like:

var json = isAssetId(jsonObject);

console.log(JSON.stringify(json));

function isAssetId(jsonObject) {

for (let key in jsonObject) {

  if(Array.isArray(jsonObject[key])){
          for (const [i ,element] of jsonObject[key].entries()) {
           jsonObject[key][i] =  isAssetId(element);
      }
  }

  else if (typeof jsonObject[key] === "object") {

  jsonObject[key] =  isAssetId(jsonObject[key]);

  } else {

     if(key=='assetId'){
  jsonObject[key]=3;

}} }

return jsonObject;

}
  • Thank you very much for the correction. However, my issue remains the same when I execute it on `NestJs` – ahmed ali Aug 05 '21 at 12:33
  • Thank you it works just fine. However, I don't want a strigify result, I need JSON object. So When I parse back like `const finalObject= JSON.stringify(result);` `console.log(JSON.parse(finalObject));` I get the same output with `object`. any idea here? – ahmed ali Aug 05 '21 at 12:55
  • @ahmedali this is normal behavior for showing the results when you use console.log() essentially it will work just fine when you try to access that property in the parsed JSON. – MediumSpringGreen Aug 05 '21 at 13:08
  • Thanks a lot, everything seems fine. – ahmed ali Aug 05 '21 at 13:41