0

I have been trying to write a function that would return an another in line property key.

Lets say we have this JSON:

'Test123': {
    'Another Test': {},
    'Test some more': {
        'Still testing?': {
            'Yeah...': {}
        },
        'Never ending story': {}
    }
},

For example, if we pass 'Still testing?' key into the function it should return 'Yeah...', but if we pass 'Yeah...' it should return 'Never ending story'. Doest someone knows who to do it? I have been trying to write a function doing that but my brain doesn't support it...

Maarduk
  • 236
  • 1
  • 11
  • What if `Yeah...` has a property called `Never ending story`? Do you want the property name of property value? – igg Mar 19 '20 at 10:22
  • 1
    Consider describing your use case wider. So far, it looks like having deeply nested object where only property names are returned (?) is somewhat inefficient. – Yevgen Gorbunkov Mar 19 '20 at 10:27
  • @igg Just the name, If Yeah would have Never ending story as a prop that wouldn't work sadly because of the user case, Yevgen Gorbunkov, I'm trying to render questions based on this JSON. We ask user the question 'Another test' he clicks yes or no, later on next question is displayed, that would be 'Test some more'. Problem happens when comes to the subquestions, if client answers yes to the Test some more question I want to display him Still testing, and, later on, after displaying Yeah I want to display him Never ending story. – Maarduk Mar 19 '20 at 10:32
  • Assuming you have an object `{ "A": {}, "C": {}, "B": {} }` and you pass in the key "A" would you expect to get back C? or B? Or does it not matter? – Olian04 Mar 19 '20 at 11:00
  • After passing A I would expect to get C, yeah :) – Maarduk Mar 19 '20 at 11:07
  • How about something like this https://jsbin.com/zedikicula/edit?js,console, isn't the cleanest of code. but just for a idea. – Code Maniac Mar 19 '20 at 11:21
  • @Maarduk That's not possible using strictly objects. Since javascript does not guarantee the order of properties. See https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – Olian04 Mar 19 '20 at 11:23

1 Answers1

1

let obj = {
    'Test123': {
        'Another Test': {},
        'Test some more': {
            'Still testing?': {
                'Yeah...': {}
            },
            'Never ending story': {}
        }
    }
}

function nextKey(keyStr, object) {
    if (!keyStr || !object || typeof object != 'object' || object.constructor.name != 'Object') {
        console.log("Improper Parameter !!!")
        return
    }
    let found = false
    let answer = {}
    findKey(object)
    return (Object.keys(answer).length) ? answer : "";

    function findKey(obj) {
        for (let key of Object.keys(obj)) {
            if(Object.keys(answer).length) return;
            let value = obj[key]
            if(key == keyStr)found = true;
            else if(found) {answer.key = key; answer.value = value; return}
            if (Object.keys(value).length) findKey(value)
        }
    }
}

console.log(nextKey('Still testing?', obj))     //{ key: 'Yeah...', value: {} }
console.log(nextKey('Yeah...', obj))            //{ key: 'Never ending story', value: {} }