在对象的嵌套数组中查找键 [英] Find keys in nested array of objects

查看:94
本文介绍了在对象的嵌套数组中查找键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I receive multiple JSONs from an API (17 API calls with Promise.all() ). For example

[
  {
    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
      }
    ]
  }
]

,我真的不知道要获得我想要的密钥(并以更通用的方法来获得它).到目前为止,我的尝试是

and I really have no idea to get the key I want (and to get it on a more generic approach). So far my attempts have been

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,因此需要一个干净而全面的解决方案,但无法自己构建一个解决方案.

Do you have some more ideas? I'm still learning JavaScript and thus want a clean and comprehensive solution, but couldn't build one by myself.

推荐答案

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

You can use below function to get all values stored at the key you specify:

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

这篇关于在对象的嵌套数组中查找键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆