0

I retrieve a list of pairs from a JSON document. I can retrieve each pair through

$.each(result,function(key,value)
{
    alert(key+" "+value);
}

but for certain cases, I would like to retrieve only each value, put it in an array so I can add it to an autocomplete function :

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

But I cannot find how to construct the object "listOfValues".

I have almost no experiences in JQuery and JSON. (That's why I assume, my question is quite simple, but I cannot find the solution)

3 Answers 3

1

You can use $.map function:

var dimensions = { width: 10, height: 15, length: 20 };
var keys = $.map( dimensions, function( value, key ) {
  return key;
});

Documentation link

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

Comments

1
var list = [];
$.each(result,function(key,value)
{
  list.push(value);
}

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

Comments

1

You can declare an Array of object to hold your listOfValues, and while iterating over your JSON result push each element to the table:

var listOfValues = [];
$.each(result,function(key,value)
{
  listOfValues.push(value);
}
//Some other JS stuff
$("#myTextField").autocomplete({
  source:listOfValues
});

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.