0

I'm trying to search a value in json

<input type="text" id="test" size="21" maxlength="120">
<button onclick="Zoek()" class="btn btn-info btn-block">
        tijdelijke zoek knop
</button>

I'm using this to input a value and the button to call the search function

function Zoek() {
    var qeustion = document.getElementById("test").value;
    document.getElementById("accordion").innerHTML == "";
    var text = '{ "FAQ" : [' +
        '{ "vraag":"John" , "antwoord":"Doe" },' +
        '{ "vraag":"Anna" , "antwoord":"Smith" },' +
        '{ "vraag":"Peter" , "antwoord":"Jones" } ]}';

    obj = JSON.parse(text);

    for (i = 0; i < text.length; i++) {
        if (obj.FAQ[i].vraag == qeustion) //(obj.FAQ[i].getString("vraag").contains(question))
        {
            document.getElementById("accordion").innerHTML += "<div class='panel panel-default'><div class='panel-heading' role='tab' id='heading" + i + "'><h4 class='panel-title'><a data-toggle='collapse' data-parent='#accordion' href='#" + i + "' aria-expanded='false' aria-controls='" + i + "''>" + obj.FAQ[i].vraag + " </a></h4></div><div id='" + i + "' class='panel-collapse collapse in' role='tabpanel' aria-labelledby='heading" + i + "'><div class='panel-body'> " + obj.FAQ[i].antwoord + "</div></div></div> WOWOWOWOWOWOWOWOWWOWOWOOW";
        } else {
            document.getElementById("accordion").innerHTML = "No results found"
        }
    }
}

and this is my search function

so lets say i enter John it goes straigt to the else and doesnt do the if statement even though i am pretty sure it kind of is right

could anyone give me some pointers on searching in a json object? is there a other way to do this?

1
  • the answer below and jsfiddle show you what you are after. Can you confirm this works as you want? the issue is that there was no break statement Commented May 12, 2015 at 0:36

2 Answers 2

1

Please see jsfiddle attached demonstrating what you are looking for and will show you what you need to do - https://jsfiddle.net/vuenume2/1/

It is essential to have a break statement in your loop.

Without the break statement your true value for success simply gets overwritten with false on the next iteration, except for the last possible credentials, for which there is no "next" iteration.

if (obj.FAQ[i].vraag == qeustion)
        {
           <!-- do stuff -->
            break;
        } else {
             <!-- do other stuff -->
        }

Also, if you haven't done so you need to add a div with an id accordion to your html

<div id="accordion"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

oh btw if you dont mind me asking. is there a option to do what i have but with something like contains. so like if i enter a 's' it will give me all the options with s in it?
0

Use filter function. You parsed in obj that string into json so You could do:

var target = obj.FAQ.filter(function(element){ return element.vraag == qeustion})[0];
if(target == undefined) { 
  // there is no that object logic 
} else {
  // there is that object logic
}

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.