[英]Taking an object and using it as a source for autocomplete
I have need of an autocomplete using Jquery UI. 我需要使用Jquery UI自动完成。 The object I have looks like this.
我的对象看起来像这样。
"last_name":"A" , "first_name":"A" , "email_addr":"A@A.COM"
“ last_name”:“ A”,“ first_name”:“ A”,“ email_addr”:“ A@A.COM”
I have no control over this. 我对此无能为力。 It's how the data is formatted when it is returned from a database query.
这是从数据库查询返回数据时格式化数据的方式。 I want to use it as a source for the autocomplete.
我想将其用作自动完成的来源。 What I have is this:
我所拥有的是:
$( "#inputVisitorLastName" ).autocomplete({
source: visitorsDrop
});
where visitorsDrop is a list of those objects. 其中visitersDrop是这些对象的列表。 What would be the best way to approach this and how would I go about doing that?
什么是解决此问题的最佳方法,我将如何去做? Is there a way that I don't know of to change the objects to include the value and label tags that they need?
有一种我不知道的方法来更改对象以包括它们所需的值和标签标记吗? Should I try to turn the objects into strings?
我应该尝试将对象变成字符串吗? Google turns up confusing results on how to do that, as well.
Google也对如何做到这一点感到困惑。 I just tried something like this:
我只是尝试过这样的事情:
var intArr = [visitorsDrop.last_name];
and the array it makes is just undefined. 它所构成的数组只是未定义的。 I'm pretty new to Javascript and not sure what's going wrong;
我刚接触Javascript,不知道出了什么问题; can anybody help me out?
有人可以帮我吗?
EDIT: Since somebody asked, visitorsDrop is assigned its value through JSON and vb.net, which makes the database query; 编辑:由于有人问,将通过JSON和vb.net将visiterDrop的值分配给数据库查询; so it's made like this:
所以它是这样的:
var visitorsDrop = that.getVisitors();
Need to turn an array of objects into an array of strings? 是否需要将对象数组转换为字符串数组? Sounds like a job for
map
! 听起来像是要做
map
的工作!
$( "#inputVisitorLastName" ).autocomplete({
source: visitorsDrop.map(function(o) { return o.last_name; })
});
foo.map(...)
builds a new array by passing each element of foo
(an existing array) into a function and using the return value of each function call as the value for that place in the new array. foo.map(...)
通过将foo
(一个现有数组)的每个元素传递到一个函数中,并将每个函数调用的返回值用作新数组中该位置的值,来构建一个新数组。 So, here, each object in visitorsDrop
is passed in as o
to the function, and you get a new array filled with the last_name
value of each object. 因此,在这里,
visitorsDrop
每个对象都以o
形式传递给函数,您将获得一个新数组,其中填充了每个对象的last_name
值。
To support older browsers, you may need to supply a polyfill . 为了支持较旧的浏览器,您可能需要提供polyfill 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.