javascript - How to stop query results being duplicated in html options drop down box? -
javascript - How to stop query results being duplicated in html options drop down box? -
using parse.com , javascript sdk.
update - i've created fiddle show issue http://jsfiddle.net/dano007/j8vgqvqg/11/
the below code returns list of friends current user connected to. displays results in options drop box user select friend. there 3 records in parse status of "connected", 3 records shown in inspect elements in chrome too.
for reason when results beingness displayed in drop box beingness duplicated.
i need include both touser , fromuser, other wise using 1 of these 1/2 friend connections.
how can stop duplication in drop downwards box? working example/anwser amazing!
![var currentuser = parse.user.current(); var friendrequest = parse.object.extend("friendrequest"); var queryone = new parse.query(friendrequest); queryone.equalto("touser", currentuser); var querytwo = new parse.query(friendrequest); querytwo.equalto("fromuser", currentuser); var mainquery = parse.query.or(queryone, querytwo); //mainquery.include('touser'); mainquery.include('fromuser'); mainquery.equalto("status", "connected"); mainquery.find({ success: function(results) { var friends = \[\]; (var = 0; < results.length; i++) { friends.push({ username: results\[i\].get('fromuser').get('username'), userid: results\[i\].get('fromuser').id, }); var select = document.getelementbyid("selectfriend"); $.each(friends, function(i, v) { var opt = v.userid; var optfrontend = v.username; var el = document.createelement("option"); el.textcontent = optfrontend; el.value = opt; select.appendchild(el); }) } }, error: function(error) { alert("error: " + error.code + " " + error.message); } }); html
<option value="0">select friend</option>
you utilize $.unique() remove duplicates array:
friends = $.unique( friends ); untested, work straight in $.each:
$.each($.unique(friends), function(i, v) { ... update: using $.unique() doesn't work friends array, perchance friends consists of unique entries having same username, different other values - possible utilize next approach:
var added = {}; $.each(friends, function(i, v) { var opt = v.userid; var optfrontend = v.username; if(!added[optfrontend]) { added[optfrontend]= true; var el = document.createelement("option"); el.textcontent = optfrontend; el.value = opt; select.appendchild(el); } }); demo fiddle
this add together username options haven't been added checking if added[optfrontend] true while processing $.each().
as sidenote - you're using jquery utilize
var select = $("#selectfriend"); instead of
var select = document.getelementbyid("selectfriend"); but have change
select.appendchild(el); into
select.append(el); update: suggested approach works when var added = {}; used in approach declared in global scope, it's accessible within each(). if it's declared above each(), within function in op code, undefined within each().
javascript jquery parse.com
Comments
Post a Comment