2

I am working with D3js library. On the web page, there is a search box which user can input their parameter. I receive arrays of data via Ajax post to my database. Following is the code:

..........
var aa;
var bb;
$(function(){
$.ajax({
    type: "POST",
    url: "http://localhost:7474/db/data/transaction/commit",
    accepts: {json: "application/json"},
    dataType: "json",
    contentType: "application/json",
    data: JSON.stringify(query), //query is somewhere above the code
    //pass a callback to success to do something with the data
    success: function (data) {
        aa = data.results[0].data;
        aa.forEach(function (entry) {
             passVar(entry.row[0].name)
        });}}
    );
});

function passVar(smth){
    bb =[smth];
    console.log (bb);
    //Should search the user input..........
    }
//if the user input matches, filter function should run.........
function filterData() {
    var value = d3.select("#constraint")[0][0].value;

    inputValue = value;

    ............
}

As the result of console.log(bb)I receive the following on console:

["Direct Digital Control System"]
["Fire Protection"]
["HVAC Cooling- Waterside"]
["HVAC Heating- Waterside"]
["HVAC System"]
["HVAC-01"]
["HVAC-02"]

What I want to do: If the user input match with one of the results in var bb, then program should run function filterdata() {....for querying. If not, don't do anything.

How should I write the code to make the search and run the other function? Thanks for the any help/suggestion.

2
  • You just want to check whether the user input is already in your array right? Commented Sep 24, 2015 at 14:05
  • Yes i wanted to check whether user input is in the array. And if so, run a function. Sample code below worked well. Thanks Commented Sep 25, 2015 at 17:09

1 Answer 1

1

You can loop through the array and find whether the user input is equals to the current index value of the array. if equals you can call your function and break the loop since no need to loop further more.

for(int i=0; i<bb.length; i++){

    if(bb[i] == userInput){

        filterdata();

        break;

    }

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

3 Comments

Code-only answers aren't the best - consider editing your answer to add some explanation of how you chose to solve the problem.
Thanks @Serlite. I put this answer using the mobile application. Thats what made the answer short because typing is difficult in mobile phone. Anyway I added some explanations. cheers
Thanks for the explanation and sample code. Worked well.

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.