views:

34

answers:

3

I am trying to find away to search in returned json result. the returned result like :

{"Result":["Css","java","jquery","asp.net","mvc","javascript","asp","c#"]}

I want to get all the words that starts with "j" in an array or another json object.

+2  A: 

Unfortunatly, there is no searchJSONandReturnAllEntrysStartingWith('J') method. But you can do it easily yourself.

function searchJSONandReturnAllEntrysStartingWith(ch){
        var jsonres = window.JSON.parse('{"Result":["Css","java","jquery","asp.net","mvc","javascript","asp","c#"]}'),
        filter  = [];

    filter = $.map(jsonres.Result, function(elem, i){
        if(elem.charAt(0) === ch) return elem;
    });

    return filter;
}

alert(searchJSONandReturnAllEntrysStartingWith('j'));
jAndy
A: 

thanks for your solution but what should I do if I have a word in a text box and I want to return all words that starts with all like the word in the text box

Mazen
A: 
var jsonres = window.JSON.parse('{"Result":["Css","java","jquery","asp.net","mvc","javascript","asp","c#"]}'),
        filter  = [];

    filter = $.map(jsonres.Result, function(elem, i){
                        var wordLength = $("#textbox1").val().length;
                        var beforeEnd = wordLength - 1;
                        var ch = $("#textbox1").val().substring(beforeEnd,wordLength);
        if(elem.charAt(beforeEnd) === ch) return elem;
    });

    return filter;

I have modify it like that if you have a better solution kindly tell me.

Mazen