Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

javascript - Find keys in nested array of objects

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

and

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }, [])
}

Call with

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...