0

I have the following JSON data feed:

jQuery191029421305245357143_1380819227858(
    {
        "responseHeader": {
            "status": 0,
            "QTime": 127
        },
        "command": "build",
        "spellcheck": {
            "suggestions": [
                "restaurant",
                {
                    "numFound": 1,
                    "startOffset": 0,
                    "endOffset": 10,
                    "suggestion": [
                        "restaurants"
                    ]
                },
                "berl",
                {
                    "numFound": 4,
                    "startOffset": 11,
                    "endOffset": 15,
                    "suggestion": [
                        "berlin",
                        "berlin brandenburg",
                        "berlin hamburg",
                        "berliner"
                    ]
                },
                "collation",
                "restaurant berlin",
                "collation",
                "restaurant (berlin brandenburg)",
                "collation",
                "restaurants berlin",
                "collation",
                "restaurant (berlin hamburg)"
            ]
        }
    }
)

and i try to access the following data in JQuery:

"restaurant berlin"
"restaurant (berlin brandenburg)"
"restaurants berlin"
"restaurant (berlin hamburg)"

I have the following JQUERY code so far:

success: function( data ) {
    response( $.map(data.spellcheck.suggestions, function(item) {    
        return item;
}));

which is working but return me all values and not only the 4 data sets outlined above.

Thank you for your help!

Jason P
  • 26,984
  • 3
  • 31
  • 45
user2843661
  • 19
  • 1
  • 5
  • 1
    your "json" looks incorrectly formatted... why do you have an array that is a mixture of objects and strings? wouldn't it be better to make it consistent by making all of them objects? – Kevin B Oct 03 '13 at 17:14
  • This is *jsonp*, right ? – DontVoteMeDown Oct 03 '13 at 17:15
  • @KevinB The json is not incorrect, I wouldn't say I'd do it like that if I could choose, but this is valid JSON, you can mix and match whatever you want inside an array. – Kenneth Oct 03 '13 at 17:17
  • i didn't say it was invalid or incorrect, it's just... not normal. it can be done using the current format, but using a more consistent format would make it much easier to work with, otherwise on each iteration you have to detect whether it's a string or an object and handle it differently for each item. – Kevin B Oct 03 '13 at 17:18

1 Answers1

0

In your callback for the jQuery map-function you can do a check and return null if you do not want the item in the result.

The following snippet shows an example of how to filter out everything that's not a string and everything that's a string equal to "collation":

success: function( data ) {
    response( $.map(data.spellcheck.suggestions, function(item) {    
        if (typeof item != "string") return;
        if (item === "collation") return;
        return item;
}));
Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • thank you!. this is what I was looking for. There is 1 last small issue: How can I remove the results "restaurant" and "berl" from the result? – user2843661 Oct 03 '13 at 18:20
  • You'd have to do some string conversion/parsing to return the correct values. In essence, the callback receives every item from the collection and creates a new collection with what you return from each call. – Kenneth Oct 03 '13 at 18:40
  • Could you first try something? This is basic programming. – Kenneth Oct 03 '13 at 19:07