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 ?

>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);

Leave a Reply