fetch the json value and assign in json using node js / javascript

let arrayId={
  "za": {
    "gasjxhj6782": {
      "value": "gasjxhj6782",
      "access": "Available at some point"
    },
    "vghasj56728": {
      "value": "vghasj56728",
      "access": "Access denied"
    },
    "ldghjd67821": {
      "value": "ldghjd67821",
      "access": "Admin role"
    }
  },
  "cn": {
    "hjaks7812": {
      "value": "hjaks7812",
      "access": "Available at some point"
    },
    "78912ashm": {
      "value": "78912ashm",
      "access": "Access denied"
    }
  },
  "tw": {
    "78912ashm": {
      "value": "78912ashm",
      "access": "Access denied"
    }
  }
};

let obj="hjaks7812";

function blockAccess(obj) {
  if (obj in arrayId) {
    attrs = {
      "type": "Access permission",
      "entry-level-id": "",
      "key": "",
      "access": "",
    };
  }
  console.log(attrs);
}

This is my function I want to check the value of obj present in arrayId and assign the value

for eg obj is hjaks7812 I want to check it present at which level of arrayId

and make my expectation output as this if the value is hjaks7812

{
  "type": "Access permission",
  "entry-level-id": "hjaks7812",
  "key": "cn",
  "access": "Available at some point",
}

but value of obj get changed maybe it can be gasjxhj6782 or any thing then it should take that value if the obj value is like this gasjxhj6782 then expectation value will be like this

{
  "type": "Access permission",
  "entry-level-id": "gasjxhj6782",
  "key": "za",
  "access": "Available at some point",
}

as I want to check obj at which level and assign that value here za cn tw get changed everytime as this is user define so I want to check at which level the value which is assign in obj is present and fill that attr

>Solution :

You can try this approach with entries and double loop

let arrayId = {
  "za": {
    "gasjxhj6782": {
      "value": "gasjxhj6782",
      "access": "Available at some point"
    },
    "vghasj56728": {
      "value": "vghasj56728",
      "access": "Access denied"
    },
    "ldghjd67821": {
      "value": "ldghjd67821",
      "access": "Admin role"
    }
  },
  "cn": {
    "hjaks7812": {
      "value": "hjaks7812",
      "access": "Available at some point"
    },
    "78912ashm": {
      "value": "78912ashm",
      "access": "Access denied"
    }
  },
  "tw": {
    "78912ashm": {
      "value": "78912ashm",
      "access": "Access denied"
    }
  }
};

function blockAccess(key) {
  for (const [arrayKey, arrayValue] of Object.entries(arrayId)) {
    for (const [accessKey, accessValue] of Object.entries(arrayValue)) {
      if (accessKey === key) {
        return {
          "type": "Access permission",
          "entry-level-id": accessKey,
          "key": arrayKey,
          "access": accessValue.access,
        };
      }
    }
  }
  return 'Not found'
}

console.log(blockAccess("hjaks7812"))
console.log(blockAccess("gasjxhj6782"))
console.log(blockAccess("testing")) //Not found if the key is not in your data

Leave a Reply