I'm writing a small js snippet that should return values based on the keys from object. The object can be nested even, here is my code.
let obj = {
  "name": "Family",
  "count": 6,
  "children": {
    "name": "Jim",
    "age": "24",
    "address": {
      "street": "5th ave",
      "state": "NY",
      "country":"US"
    }
  },
  "parents": {
    "name": "John",
    "age": "45",
    "address": {
      "street": "New brew st",
      "state": "CA",
      "country":null
    }
  }
};
const getData = (obj) => {
  let jsonString = obj;
  let keys = ["name", "count","children.address.street"];
  let returnString = "";
  keys.forEach((item) => {
    returnString += buildString(item, jsonString) + "$$";
  })
  return returnString.replace(/(\$\$)*$/, "");
}
const buildString = (keyIn, data) => {
  var value = '';
  if (keyIn.includes('.'))
    value = splitAndReturn(keyIn, data);
  else
    value = data[keyIn] ? data[keyIn] : null;
  return `${keyIn}###${value}`;
};
const splitAndReturn = (keyIn, obj) => {
  let nKeyArr = keyIn.split('.');
  let buildString = 'obj';
  nKeyArr.forEach(item => {
    buildString += `['${item}']`
  });
  let nKey = buildString;
  return (nKey)
}
console.log(getData(obj));I'm thinking in this way, whenever there is a . in my keys, I want to call a new function that will be converted into [''] format and at the end return obj[''].
for eg. If I pass in children.name I should get Jim, and If I pass in children.address.country I should get US.
But currently, I'm returning a string, I am not sure how can I get the result of the key. Can someone please let me know how can I achieve this?
 
    