Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

accessing JSON value in array

I have the following JSON object

[
  {
    'parameter-name': 'device',
    enabled: true,
    value: '077743322L102515',
    description: 'device identifier. Should be used only when multiple devices are connected at once'
  },
  {
    'parameter-name': 'app_id',
    enabled: true,
    value: 'com.instagram.andrpbj',
    description: ' when using this parameter, you are able to use Insomniac on a cloned Instagram-application. Just provide the new package name'
  },
  {
    'parameter-name': 'old',
    enabled: false,
    value: 'True',
    description: 'add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version'
  }
]

i wanted to access the value of the parameter-name: ‘old’, by value i mean value
is there a one step solution to do that without iterating through each entry ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From my point of view, using Array.find() is the cleanest solution to get the value:

const { value } = data.find((obj) => obj['parameter-name'] === 'old');

If your goal is to edit the value as you’re asking in the comment, you can get the index of the targeted object within the array using Array.findIndex() and then edit the data:

const objIdx = data.findIndex((obj) => obj['parameter-name'] === 'old');
data[objIdx].value = 'newValue'

EDIT

data.find(ob => ob["parameter-name"] === 'old').value = "new value";

Code Snippet

const data = [{
    'parameter-name': 'device',
    enabled: true,
    value: '077743322L102515',
    description: 'device identifier. Should be used only when multiple devices are connected at once'
  },
  {
    'parameter-name': 'app_id',
    enabled: true,
    value: 'com.instagram.andrpbj',
    description: ' when using this parameter, you are able to use Insomniac on a cloned Instagram-application. Just provide the new package name'
  },
  {
    'parameter-name': 'old',
    enabled: false,
    value: 'True',
    description: 'add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version'
  }
];

data.find(ob => ob["parameter-name"] === 'old').value = "Value is now set to False";

console.log(data);

In order to account for a situation where the ‘searched-value’ is not found, it may be better to split the assignment like this:

const foundObj = data?.find(ob => ob["parameter-name"] === 'old');
if (foundObj) foundObj.value = "Value is now set to False";
const data = [{
    'parameter-name': 'device',
    enabled: true,
    value: '077743322L102515',
    description: 'device identifier. Should be used only when multiple devices are connected at once'
  },
  {
    'parameter-name': 'app_id',
    enabled: true,
    value: 'com.instagram.andrpbj',
    description: ' when using this parameter, you are able to use Insomniac on a cloned Instagram-application. Just provide the new package name'
  },
  {
    'parameter-name': 'old',
    enabled: false,
    value: 'True',
    description: 'add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version'
  }
];

const foundObj = data?.find(ob => ob["parameter-name"] === 'old');
if (foundObj) foundObj.value = "Value is now set to False";

console.log(data);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading