【问题标题】:Find JSON array element value is null or undefined from multiple JSON objects从多个 JSON 对象中查找 JSON 数组元素值为 null 或未定义
【发布时间】:2021-04-08 12:15:37
【问题描述】:

我有一组具有 wsDestAddress 键的 JSON 对象,那么如何遍历 JSON 对象或在现有 JSON 对象中疯狂搜索 wsDestAddress 键以查找键是否存在并返回 @ 987654323@或""

JSON 对象 #1

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

JSON 对象 #2

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

JSON 对象 #3

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

JSON 对象 #4

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

我知道这段代码可以正常工作

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

但我想疯狂搜索wsDestAddress 作为 JSON 键搜索。

【问题讨论】:

    标签: javascript json angular typescript


    【解决方案1】:

    这是使用object-scan 的答案。您的要求并不完全清楚,但我相信您可以轻松调整以下代码以满足您的需求。

    // 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>

    免责声明:我是object-scan的作者

    【讨论】:

      【解决方案2】:

      您可以通过以下方式找到"wsDestAddress" 值为"" 的第一项:

      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

      【讨论】:

        猜你喜欢
        • 2017-04-15
        • 2013-09-15
        • 2017-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-22
        • 2018-09-17
        • 2019-09-30
        相关资源
        最近更新 更多