Traversing an object getting the key and all parents keys
traverse tree(json), fulfill getKeys(data, str) function using JS. get the key and all parents keys. const data = { key1: 'str1', key2: { key3: 'str3', key4: 'str4', key5: { key6: 'str6', key7: 'str7', key8: 'str8', }, } } for example: getKeys(data, 'str1'); return: 'key1' getKeys(data, 'str3'); return: 'key2, key3' getKeys(data, 'str6'); return: 'key2, key5, key6' I think it can be done be recursion, but how? this is my solution, but failed let s = []; function getKeys(data, str, key='') { if (key !== '') { s.push(key); } for (item in data) { if (typeof data[item] === 'object') { getKeys(data