2

I'm trying to use the jQuery UI Autocomplete widget with a custom JSON feed I'm getting back from an API, which is formatted as follows:

    {

    "SearchTerm": "ches",
    "HasDirectCountyHit": false,
    "DirectCountyHitId": null,
    "HasDirectLocationHit": false,
    "DirectLocationHitId": null,
    "Developments": [
        {
            "Id": "45339ae3e55a",
            "Label": "Chestnut Walk, Bilston",
            "Url": "/developments/chestnut-walk-bilston"
        },
        {
            "Id": "4835f52e053a",
            "Label": "Crown Park, Chester",
            "Url": "/developments/crown-park-chester"
        },
        {
            "Id": "757964964cc6",
            "Label": "The Birches, West Timperley",
            "Url": "/developments/the-birches-west-timperley"
        }
    ],
    "Counties": [
        {
            "Id": "7",
            "Label": "Cheshire",
            "Url": "/search?cid=7"
        },
        {
            "Id": "24",
            "Label": "Greater Manchester",
            "Url": "/search?cid=24"
        }
    ],
    "Locations": [
        {
            "Id": "12061",
            "Label": "Cheselbourne, Dorset (DT2 7)",
            "Url": "/search?lid=12061"
        },
        {
            "Id": "12062",
            "Label": "Chesham, Buckinghamshire (HP5 1)",
            "Url": "/search?lid=12062"
        },
        {
            "Id": "12063",
            "Label": "Chesham, Greater Manchester (BL9 6)",
            "Url": "/search?lid=12063"
        },
        {
            "Id": "12064",
            "Label": "Chesham Bois, Buckinghamshire (HP6 5)",
            "Url": "/search?lid=12064"
        },
        {
            "Id": "12065",
            "Label": "Cheshunt, Hertfordshire (EN8 9)",
            "Url": "/search?lid=12065"
        },
        {
            "Id": "12066",
            "Label": "Chesley, Kent (ME9 7)",
            "Url": "/search?lid=12066"
        },
        {
            "Id": "12067",
            "Label": "Cheslyn Hay, Staffordshire (WS6 7)",
            "Url": "/search?lid=12067"
        },
        {
            "Id": "12068",
            "Label": "Chessetts Wood, Warwickshire (B94 6)",
            "Url": "/search?lid=12068"
        },
        {
            "Id": "12069",
            "Label": "Chessington, Kingston upon Thames - Greater London (KT9 2)",
            "Url": "/search?lid=12069"
        },
        {
            "Id": "12070",
            "Label": "Chessmount, Buckinghamshire (HP5 1)",
            "Url": "/search?lid=12070"
        }
    ]

}

The API I'm calling returns results based on my search term, so I know that all of the results in the nested objects are matches - my problem is how to access these objects ('Developments', 'Counties' and 'Locations') so that the autocomplete widget can pick up the 'Label' values?

Thanks, Robin

2
  • I think you can loop through the properties (developments,counties and locations) or use $.merge to merge all the records to one which will create a all new array with all the label, id and url values and then assign it as source to the autocomplete. Commented Oct 9, 2014 at 13:17
  • Your question is good what you have tried so far? Commented Oct 9, 2014 at 13:17

1 Answer 1

4

Ok - here's what you can do:

//put all the keys you want to pull out of your json in an array
var props = [
    "Locations", "Counties", "Developments"
];
//empty array for your autocomplete
var labels = [];

//loop thru all the properties you care about
$.each(props, function () {
    $.each(source[this], function () {
        //and pull out all the labels and add them to the labels array
        labels.push(this.Label)
    });
});


$("#autocomplete").autocomplete({
    source: labels
});

and to see it all in action I created a quick fiddle http://jsfiddle.net/fr5yb3n0/

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

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.