1

I am using the Craft Prune plugin (https://github.com/mattstauffer/craftcms-prune) to pull out a JSON array of entries from a section of my site with the aim of integrating the data into a typeahead search field as autocomplete data.

The problem I seem to be having is that the JSON array structure needs to be simplified for use as a datasource with typeahead.

So the current JSON data structure is:

[{"title":"Entry One Title"}, {"title":"Entry Two Title"}, {"title":"Entry Three Title"}]

What I actually need the structure to be is:

["Entry One Title", "Entry Two Title", "Entry Three Title"]

I have already tried using the jQuery $.each() function and the jQuery $.map() function but they both simply return an array of objects not a simple JS array.

Any help appreciated.

2

Ended up working out a solution:

var myData = {{ craft.entries.section('sectionName').find() | prune(['title']) | json_encode() | raw  }};
    var myArray = $.makeArray(myData);
    var newArray = new Array();
    $.each( arr, function(key, value) {
        $.each(value, function(keyNew, valueNew){
            newArray.push(valueNew);
        });
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.