0

i have this obj

  var myObj =   {
    "sections": [{
        "id": "s1",
        "name": "my section no.1",
        "sheets": [{
            "id": "sheet1",
            "questions": [{
                "id": "q1",
                "name": "my q",
                "options": [{
                    "id": "o1",
                    "order_no": 1,
                    "name": "option 1"
                }, {
                    "id": "o2",
                    "name": "option 2"
                }]
            }]
        }]
    }]
};

i want function to search this array by id so searchById("o1") should return

{"id": "o1","order_no": 1,"name": "option 1"}

and searchById("q1") should return

{
    "id": "q1",
    "name": "my q",
    "options": [{
        "id": "o1",
        "order_no": 1,
        "name": "option 1"
    }, {
        "id": "o2",
        "name": "option 2"
    }]
}

please help me

3 Answers 3

4

What you have is object and you can create recursive function using for...in loop to return result.

var obj = {
  "sections": [{
    "id": "s1",
    "name": "my section no.1",
    "sheets": [{
      "id": "sheet1",
      "questions": [{
        "id": "q1",
        "name": "my q",
        "options": [{
          "id": "o1",
          "order_no": 1,
          "name": "option 1"
        }, {
          "id": "o2",
          "name": "option 2"
        }]
      }]
    }]
  }]
}

function findObj(data, id) {
  for (var i in data) {
    if (i == 'id' && data[i] == id) return data
    if (typeof data[i] == 'object' && findObj(data[i], id)) return findObj(data[i], id)
  }
}

console.log(findObj(obj, 'o1'))
console.log(findObj(obj, 'q1'))

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

4 Comments

your function not iterating all keys so i posted new answer find it out
no i added a condition before return in second if clause if (typeof data[i] == 'object' && findObj(data[i], id))
What is that doing? Also could you provide example of data in jsfiddle.net where my code fails so i could fix it?
the function will skip any key after objects
0
 myArray =    {
    "sections": [{
        "id": "s1",
        "name": "my section no.1",
        "sheets": [{
            "id": "sheet1",
            "questions": [{
                "id": "q1",
                "name": "my q",
                "options": [{
                    "id": "o1",
                    "order_no": 1,
                    "name": "option 1"
                }, {
                    "id": "o2",
                    "name": "option 2"
                }]
            }]
        }]
    }]
}


 myArray = JSON.parse(myArray).sections;
   function getMeObjById(criteria) {
     var myObj = myArray.filter(funcion(obj){
      if(obj.id !== 'undefined' && obj.id === criteria){
       return true;
      }
     });
     return myObj; // or return myObj[0] if id are unique
    }

1 Comment

Can you provide an explanation for this ?
0
function findID(id, object){
    let _temp;
    // Verify if is Array
    if(object instanceof Array){
        // Scan all objects by doing recursion
        for(let i = 0; i < object.length; i++){
            if((_temp = findID(id, object[i]))){
                return _temp;
            }
        }
    }   

    // Verify if is Object
    if(object instanceof Object){
        // Verify if is the Object ID
        if(object.id && object.id === id){
            return object;
        }
        // Scan all objects by doing recursion
        for(index in object){
            if(object[index] instanceof Object || object[index] instanceof Array){
                if((_temp = findID(id, object[index]))){
                    return _temp;
                }
            }   
        }
    }
}

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.