0

Recently I'm studying jQuery . I wish that when I write here <input type="text" id="scrivo" /> I want to filter an array derived from a json file. The json file is like this:

{
  "Re": 
[
  {
    "nm": "Edward the Elder",
    "cty": "GB",
    "hse": "House of Wessex",
    "yrs": "899-925"
  },
  {
    "nm": "Edgar",
    "cty": "GB",
    "hse": "House of Wessex",
    "yrs": "959-975"
  },
  {
    "nm": "Edward the Martyr",
    "cty": "GB",
    "hse": "House of Wessex",
    "yrs": "975-978"
  }
 ]
}

and the piece of code is this:

<script language="JavaScript">
$(document).ready(function()             
    {
      $('#scrivo').keyup(function()
        {
         var json = (function () {
         var json = null;
         $.ajax({
             'async': false,
             'global': false,
             'url': "re.json",
             'dataType': "json",
             'success': function (data) {
                 json = data;
             }
               });
             return json.Re;
         })();
         var o = $.grep(json, function (n) { return (n.nm.indexOf("El")) },true);
      });
 });
</script>

For example I would like to filter the objects that contain "El", which is the first object in the json file. Instead I do not receive anything. Can you help me please?

4
  • 1
    Forget my older comment, I didn't see you were using async : false. Maybe this question will help you? Commented Jul 26, 2014 at 15:57
  • ajax is asynchronous. You first return json.re, which is not defined, and after a while response comes from server. Commented Jul 26, 2014 at 16:02
  • 1
    why async:false?? Commented Jul 26, 2014 at 16:02
  • 1
    It's because return n.nm.indexOf("El") should be return n.nm.indexOf("El") !== -1. The indexOf method returns an index, which is -1 when no match is found. So the first object returns a higher index and all the others return -1, all of which are "truthy" values. This means the condition always passes, and because you passed true as the third argument to $.grep, the condition is inverted, removing excluding all items. Commented Jul 26, 2014 at 16:18

1 Answer 1

1

I found the bug: You have inverted the grep command. Here's a jsFiddle demonstrating my answer: http://jsfiddle.net/UqE5n/3/

The crucial line is here:

var value = $('#scrivo').val();
var o = $.grep(jsonRe, function (n) {
    return n.nm.indexOf(value) >= 0;
}, false);                                <--- "true" will invert the result!

It will also guide you how to use jsFiddle for asking with a code example. I am learning jsQuery myself, so thanks for the question!

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

2 Comments

Thanks! I tried with false but the results was wrong. So the point is >= 0
happy to have helped you. please consider upvoting.

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.