Search code examples
javascriptnode.jsarraysjsonnodes

assign value by matching the json key and value of another json using javascript / node js


I am trying to assign a json value by comparing the nested json object deeply

my first json object result look like this when I do console.log(result);

result

{
  "computer": {
    "en": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "gallery": "Some value here",
        "dummykey": "some dummy value here"
      },
      "ksjdfsk123233165":{
        "gallery": "test value",
        "dummykey": "checking the new file here"
      }
    }
  },
  "testing": {
    "hi": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "access": "not available",
        "final": "still in progress"
      }
    },
    "ja": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "mediaaccess": "system check",
        "access": "available",
        "final": "done"
      },
      "sdjksd9120812nmdns9": {
        "access": "available",
        "final": "progress"
      }
    }
  },
  "hardware": {
    "us": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "not available"
      },
      "sjkjsp3253m899as0": {
        "fieldtest": null,
        "media": "not available"
      }
    },
    "eu": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "available"
      },
      "sjkjsp3253m899as0": {
        "fieldtest": "in test we are planning",
        "media": "not available"
      }
    }
  },
  "software": {
    "uk": {
      "2eAmIIuG4xkLvatkU3RUSy": {
        "testfield": "its working",
        "assign": "not yet"
      },
      "wehjbe8230291929umsnd":{
        "testfield": "working",
        "assign": "new trainee"
      }
    }
  },
  "demo": {
    "ga": {
      "4zu7f2YP4cXqR6aeeMxEt8": {
        "newkey": "hello",
        "assign": "to new assignnie"
      },
      "ksdjfns4575634n":{
        "newkey": "working",
        "assign": "new trainee"
      }
    },
  }
};

the second json object which I am trying to compare and assign the value is below

compareObj

{
  "computer": { "compare": "gallery" },
  "testing": { "compare": "mediaaccess" },
  "hardware": { "compare": "fieldtest" },
  "software": { "compare": "testfield" },
  "demo": { "compare": "assign" }
}

I am trying to check and assign the value which is present inside the compareObj object with the key which is present in side result obj and assign the value in compareValue

let compareValue;

but the problem is while assigning the value I want to check if the compareObj value is present inside the result obj or not

here in the first example result

"computer": {
    "en": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "gallery": "Some value here",
        "dummykey": "some dummy value here"
      }
    }
  },

and compareObj

 "computer": { "compare": "gallery" },

here compareObj contain the compare key which have gallery and the result obj contain gallary as a key so compareValue should contain the compareValue= "Some value here" like this

but in the second example

 "testing": {
    "hi": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "access": "not available",
        "final": "still in progress"
      }
    },
    "ja": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "mediaaccess": "system check",
        "access": "available",
        "final": "done"
      }
    }
  },

and compareObjObj

 "testing": { "compare": "mediaaccess" },

as testing hi does not contain the mediaaccess then it should check the other object which is ja and assign the value like this compareValue="system check"

3rd example result

  "hardware": {
    "us": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "not available"
      }
    },
    "eu": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "available"
      }
    }
  },

and compareObj

"hardware": { "compare": "fieldtest" },

if the value present in compareObj is not available inside the result obj or the value contain is null then it should look like this compareValue= "Untitled"

if the object contain multiple value then it should take value from the first object for eg in 2nd example the mediaaccess is not present so it should take from 2nd language or the 3rd language or the 4th language if in the available language the mediaaccess not present then it should print Untitled


Solution

  • const findCompareValue = (type) => {
      const searchKey = compareObj[type]?.compare;
    
      const typeObject = result[type] ?? {};
      let compareValue = null;
      outer:
      for (key in typeObject) {
        const value = typeObject[key];
        for (subkey in value) {
          const subvalue = value[subkey];
          compareValue = subvalue[searchKey];
          if (compareValue != null)
            break outer;
        }
      }
      return compareValue ?? "Untitled";
    };
    
    console.log(findCompareValue("computer")); //"Some value here"
    console.log(findCompareValue("fieldtest")); //"Untitled"
    console.log(findCompareValue("testing")); //"system check"
    

    You didn't explain what you were trying to do. The first key under the type seems like a language code. If that's the case, logic which extract a string value from the first language that contains it seems incorrect. If your question explained, at a higher level, what you are trying to achieve, the answers would probably be more useful to you.

    CodePen