Home > OS >  accessing JSON value in array
accessing JSON value in array

Time:05-02

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 ?

CodePudding user response:

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'

Instead of getting the index, you could even manipulate the value on the object directly:

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

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'
  }
];

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

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((obj) => obj['parameter-name'] === 'old');
if (foundObj) foundObj.value = 'newValue';

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((obj) => obj['parameter-name'] === 'old');
if (foundObj) foundObj.value = 'newValue';

console.log(data);

CodePudding user response:

An alternative to Alexander's answer if you don't want to mutate the array object directly.

It's a very generic function that accepts a query object (key, oldValue, newValue) so it will work on all the property keys, not just parameter-name.

It finds the object that matches the query criteria (if nothing is found the function returns null), filters out the objects that don't match the query criteria, and then returns that filtered array with a new updated object.

const arr=[{"parameter-name":"device",enabled:!0,value:"077743322L102515",description:"device identifier. Should be used only when multiple devices are connected at once"},{"parameter-name":"app_id",enabled:!0,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:!1,value:"True",description:"add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version"}];

// Accept an array, and a query object
function change(arr, query) {

  // Destructure the query
  const { key, oldValue, newValue } = query;

  // Look for the object where the value
  // of the key specified in the query
  // matches the oldValue
  const found = arr.find(obj => {
    return obj[key] === oldValue;
  });

  // If there isn't a match return null
  if (!found) return null;

  // Otherwise `filter` out the objects that
  // don't match the criteria...
  const filtered = arr.filter(obj => {
    return obj[key] !== oldValue;
  });

  // Destructure all the object properties
  // away from the property we want to update
  const { [key]: temp, ...rest } = found;
  
  // Return a new array with a new updated object
  return [ ...filtered, { [key]: newValue, ...rest } ];

}

const query = {
  key: 'parameter-name',
  oldValue: 'old',
  newValue: 'new'
};

console.log(change(arr, query));

const query2 = {
  key: 'value',
  oldValue: '077743322L102515',
  newValue: '999999999'
};

console.log(change(arr, query2));

CodePudding user response:

data.filter((value) => {
  if(value['parameter-name'] === 'old'){
     console.log(value);
  }
})
  • Related