【问题标题】:Find keys in nested array of objects在嵌套对象数组中查找键
【发布时间】:2017-12-24 18:51:16
【问题描述】:

我从一个 API 收到多个 JSON(17 个 API 调用 Promise.all())。例如

[
  {
    key: value,
    key: value,
    key: value,
    key: value,
    values: [
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
    ]
  }
  {
    key: value,
    key: value,
    key: value,
    key: value,
    values: [
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
    ]
  }
  {
    key: value,
    key: value,
    key: value,
    key: value,
    values: [
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
      {
        key: value,
        key: value,
        key: value,
        keyIWant: value
      }
    ]
  }
]

而且我真的不知道要获得我想要的密钥(并以更通用的方法获得它)。到目前为止,我的尝试已经

static _findKey(nestedData) { 
    const result = [];
    const buffer = [];
    for (const prop in nestedData) {
        const value = nestedData[prop];
        if (typeof value === "object") {
            buffer.push(Class._findKey(value)); 
        }
        if (prop === "keyIWant") { // key would be an argument from the function if it'd worked 
             result.push(value);   // doesn't work because of recursive call?
        }
    }
    return result;
}

static _findKey(projects) { // 
    return projects.forEach(project => {
        return project.values.forEach(projectValue => {
            return projectValue.key;
        });
    });
}

您还有其他想法吗?我仍在学习 JavaScript,因此想要一个干净而全面的解决方案,但无法自己构建。

【问题讨论】:

标签: javascript node.js ecmascript-6


【解决方案1】:

您可以使用以下函数来获取存储在您指定的键处的所有值:

function getKeyValues(arr, key) {
    return arr.reduce((a,b) => {
        let keys = Object.keys(b);
        keys.forEach(v => {
            if (Array.isArray(b[v])) a = a.concat(getKeyValues(b[v], key));
            if (v === key) a = a.concat(b[v]);
        });
        return a;
    }, [])
}

打电话给

getKeyValues(arr, "keyIWant")

let arr = [{
    key: 'foo',
    values: [{
        key: 'value',
        keyIWant: 'keyIWant1'
      },
      {
        key: 'value',
        keyIWant: 'keyIWant2'
      }, {
        key: 'value',
        keyIWant: 'keyIWant3'
      }
    ]
  },
  {
    key: 'foo',
    values: [{
        key: 'value',
        keyIWant: 'keyIWant4'
      },
      {
        key: 'value',
        keyIWant: 'keyIWant5'
      }, {
        key: 'value',
        keyIWant: 'keyIWant6'
      }
    ]
  }
];

function getKeyValues(arr, key) {
  return arr.reduce((a, b) => {
    let keys = Object.keys(b);
    keys.forEach(v => {
      if (Array.isArray(b[v])) a = a.concat(getKeyValues(b[v], key));
      if (v === key) a = a.concat(b[v]);
    });
    return a;
  }, []);
}


console.log(getKeyValues(arr, "keyIWant"));

【讨论】:

    【解决方案2】:

    我们将object-scan 用于许多像您这样的数据处理任务。一旦你把头绕在它周围,它就会很强大。以下是您如何回答您的问题

    // const objectScan = require('object-scan');
    
    const find = (input) => objectScan(['**.keyIWant'], { rtn: 'value' })(input);
    
    const arr = [{ key: 'foo', values: [{ key: 'value', keyIWant: 'keyIWant1' }, { key: 'value', keyIWant: 'keyIWant2' }, { key: 'value', keyIWant: 'keyIWant3' }] }, { key: 'foo', values: [{ key: 'value', keyIWant: 'keyIWant4' }, { key: 'value', keyIWant: 'keyIWant5' }, { key: 'value', keyIWant: 'keyIWant6' }] }];
    
    console.log(find(arr));
    /* =>
    [ 'keyIWant6',
      'keyIWant5',
      'keyIWant4',
      'keyIWant3',
      'keyIWant2',
      'keyIWant1' ]
    */
    .as-console-wrapper {max-height: 100% !important; top: 0}
    <script src="https://bundle.run/object-scan@13.8.0"></script>

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2017-09-10
      • 2020-10-14
      相关资源
      最近更新 更多