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...

7
  • What if Yeah... has a property called Never ending story? Do you want the property name of property value? Commented Mar 19, 2020 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. Commented Mar 19, 2020 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. Commented Mar 19, 2020 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? Commented Mar 19, 2020 at 11:00
  • After passing A I would expect to get C, yeah :) Commented Mar 19, 2020 at 11:07

1 Answer 1

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: {} }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.