1

I have a set of JSON objects having the wsDestAddress key, so how do I traverse the JSON objects or do a wild search for wsDestAddress key in present JSON objects to find the key is present or not and returning null or ""?

JSON Object #1

{
  "gfutd": {
    "wsRequestData": {
      "wsDestAddress": ""
    }
  }
}

JSON Object #2

{
  "igftd": {
    "wsRequestData": {
      "wsDestAddress": ""
    }
  }
}

JSON Object #3

{
  "y7igfutd": {
    "wsResponseData": {
      "wsDestAddress": ""
    }
  }
}

JSON Object #4

{
  "y7igptdf": {
    "wsRequestData": {
      "returnAddress": {
        "wsDestAddress": ""
      }
    }
  }
}

I know this code works fine

if (y7igfutd.wsRequestData.wsDestAddress == "" ||
  igftd.wsRequestData.wsDestAddress == "" ||
  y7igfutd.wsResponseData.wsDestAddress == "" ||
  y7igfutd.wsRequestData.returnAddress.wsDestAddress == "") {
  return "result"
}

But I want to do a wild search for wsDestAddress as the JSON keys search.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
user9414660
  • 106
  • 1
  • 2
  • 15

2 Answers2

1

You can find the first item that has a "wsDestAddress" value of "" via:

const data = [
  { "y7igfutd" : { "wsResponseData" : { "wsDestAddress" : "" }}},
  { "igftd"    : { "wsRequestData"  : { "wsDestAddress" : "" }}},
  { "y7igfutd" : { "wsResponseData" : { "wsDestAddress" : "" }}},
  { "y7igptdf" : { "wsRequestData"  : { "returnAddress" : { "wsDestAddress" : "" }}}}
];

 // Adapted from: https://stackoverflow.com/a/40604638/1762224
const findValue = (object, key) => {
  let value;
  Object.keys(object).some(k => {
    if (k === key) {
      value = object[k];
      return true;
    }
    if (object[k] && typeof object[k] === 'object') {
      value = findValue(object[k], key);
      return value !== undefined;
    }
  });
  return value;
};

const oneMatches = (arr, key, value) =>
  arr.some(item => findValue(item, key) === value);
  
const allMatches = (arr, key, value) =>
  arr.every(item => findValue(item, key) === value);
  
console.log(oneMatches(data, 'wsDestAddress', '')); // Some  = true
console.log(allMatches(data, 'wsDestAddress', '')); // Every = true
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

Here is an answer using object-scan. Your requirements were not entirely clear, but I'm sure you can easily adjust the below code to meet your needs.

// const objectScan = require('object-scan');

const data1 = { gfutd: { wsRequestData: { wsDestAddress: '' } } };
const data2 = { igftd: { wsRequestData: { wsDestAddress: '' } } };
const data3 = { y7igfutd: { wsResponseData: { wsDestAddress: '' } } };
const data4 = { y7igptdf: { wsRequestData: { returnAddress: { wsDestAddress: '' } } } };
const data5 = { y7igptdf: { wsRequestData: { returnAddress: { other: '' } } } };

const search = objectScan(['**.wsDestAddress'], {
  filterFn: ({ value }) => value === '',
  rtn: 'bool',
  abort: true
});

console.log(search(data1));
// => true
console.log(search(data2));
// => true
console.log(search(data3));
// => true
console.log(search(data4));
// => true
console.log(search(data5));
// => false
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@14.0.0"></script>

Disclaimer: I'm the author of object-scan

vincent
  • 1,953
  • 3
  • 18
  • 24