0

I'm trying to change

results = $("x", xmlResponse).map(function() {

to

results.push = $("x", xmlResponse).map(function() {

but doing so prevents any Autocomplete suggestions from appearing.

If I simply remove "push" again, the Autocomplete suggestions appear correctly with no issue.

How can I use results.push properly here?

Teli
  • 57
  • 6
  • 1
    What's "results" currently defined as? Try adding `var results = []` before the `.push` – freedomn-m Jan 06 '16 at 02:16
  • ` var results = [];` is defined globally but the problem persists -- the Autocomplete suggestions don't appear if I use 'push'. – Teli Jan 06 '16 at 02:18
  • 3
    If `results` is `[]`, then `results.push` is a *method* - you are supposed to *call* it (`results.push(element)`), not *reassign* it (`results.push = element`). And that will probably not do what you want, since it will push the whole array you get from `map` as an element to `results`, not individual elements - for that, you would want `results.push.apply(results, arrayOfElements)`. – Amadan Jan 06 '16 at 02:19
  • 1
    Is there a reason your wanting to go from = to push()? Because results.slice(0, 5) is acting on an array. And if you [].push($.map()) your array will have a single element, which is an array. Your probably after [].concat($.map(...)) instead. – Taplar Jan 06 '16 at 02:25
  • @Taplar The reason is that I am going to use another AJAX function right after, which is almost identical but "GetMovies" instead of "GetGames". Therefore, "results" must contain the combined results of both AJAX calls so that the Autocomplete will suggest both Movies and Games. – Teli Jan 06 '16 at 02:28
  • 2
    I think you want concat() – Taplar Jan 06 '16 at 02:28
  • 1
    @Teli _"The reason is that I am going to use another AJAX function right after, which is almost identical but "GetMovies" instead of "GetGames""_ Can include second AJAX function at Question ? – guest271314 Jan 06 '16 at 02:33
  • @guest271314 I have now updated the post to include the second function. Thank you – Teli Jan 06 '16 at 02:41
  • @Taplar Is there something wrong with my syntax here? http://pastebin.com/JYMa7Dwp `results.concat( $("Game", xmlResponse).map(function() { return { value: $("GameTitle", this).text() + ", " + ($.trim($("ReleaseDate", this).text()) || "(unknown date)") }; }) ); ` – Teli Jan 06 '16 at 03:00
  • 2
    Someone mentioned below, $.fn.map (which is what your using since your doing map off of a lookup) returns a jQuery object containing the result set. To get a basic array do get() off of it. – Taplar Jan 06 '16 at 03:07

2 Answers2

1

push is a method, not a property .

You can't use it as a property.

results.push = $("Game", xmlResponse).map(function() {

In order to push element into results , results have to be an array

Like this

var results =[];
results.push("your things");
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
1

Use .get() to return an array from .map()

results = $("Game", xmlResponse).map(function() {
    return {
        value: $("GameTitle", this).text() 
               + ", " 
               + ($.trim($("ReleaseDate", this).text()) || "(unknown date)")
    };
}).get()
guest271314
  • 1
  • 15
  • 104
  • 177
  • What might be the proper syntax for .get() in the GetMovie function? I'm trying `results = $.map(list, function(v,i){ return { label: v.Title + ' (' + v.Year + ')', value: v.Title }; }).get()` but I get the error "TypeError $.map(...) is not a function" – Teli Jan 06 '16 at 03:16
  • 1
    `.get()` is not necessary if use `$.map()` – guest271314 Jan 06 '16 at 03:18