/    Sign up×
Community /Pin to ProfileBookmark

Distinct command

Is there a method in JavaScript to select the distinct values of an XML file?

TIA

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@96turnerriJun 04.2004 — you could try

document.getElementByTagName('TAGNAME').value
Copy linkTweet thisAlerts:
@_J_authorJun 04.2004 — Thanks 96turnerri, I'm afraid that doesn't work.

I'm working on a workaround, which hopefully will produce a solution, albeit a bit of an elasta-plast solution.

The problem is, is that my code is...

[code=php]
function verifiyDistName(distName)
{
var matchedNodes = xmlDoc.selectNodes("//DistributionLists//*[ contains(translate(N, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), translate('" + distName + "', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))]");
if (matchedNodes.length==1)
{
fraRecipients.document.body.innerHTML = fraRecipients.document.body.innerHTML + "<FONT color=#0000FF>" + matchedNodes[0].childNodes[1].text + "</FONT>,"
Form1.txtRecipientIDString.value = Form1.txtRecipientIDString.value + matchedNodes[0].childNodes[0].text + ","
Form1.txtRecipientsString.value = Form1.txtRecipientsString.value + matchedNodes[0].childNodes[1].text + ","
Form1.btnSend.disabled=false;
Form1.btnSave.disabled=false;
return true
}
else if (matchedNodes.length>1)
{


for (var i=0; i<Form1.ddlDistNamesFound.options.length;i++)
{
Form1.ddlDistNamesFound.options[i]=null;
i--;
}
for (var loop=0 ; loop<matchedNodes.length ; loop++)
{
var newElem = document.createElement("OPTION");
newElem.text = matchedNodes[loop].childNodes[1].text;
newElem.value = matchedNodes[loop].childNodes[0].text;
Form1.ddlDistNamesFound.add(newElem);
}

}
else if (matchedNodes.length==0)
{
fraRecipients.document.body.innerHTML = fraRecipients.document.body.innerHTML + "<FONT color=#FF0000>" + distName + "</FONT>,"
Form1.chkErrorFoundInRecipients.checked=true
return false
}
}

[/code]


Without working it out, I pass in a string to the function, and it traverses the XML document looking for matches. If it finds one, it changes the entered text into the found text.

If it doesn't find any, then it highlights the entered text as red.

If it finds more than one, it populates a dropdown list with each match, awaiting for the user to select an alternative.

Short, sweet, but pretty user-friendly even if I say so myself... lol.

The problem is, is that you could have for example "Bob Smith" in a different distribution list, in which case the following could happen...

User enters Bob.

System finds 5 instances of Bob [Bob Smith, Bob Jones, Bob Ball, Bob Ball, Bob Jones]

User selects Bob Smith

System changes Bob to Bob Smith

*User checks again

System Finds Bob Smith in two distribution lists

System shows Bob Smith twice

User Selects Bob Smith (again!)

Goto *
...!!!

Hence why I wanted to return distinct names.

The solution I'm working on, is when the dropdown list is populated, to then filter out any duplicates, and if the length of the dropdown list is greater than 1, then show the alternatives, otherwise it must be a single-uniquely found name, and thus is correct...!

Phew!!
Copy linkTweet thisAlerts:
@_J_authorJun 04.2004 — Well, I now have...

[code=php]
// Filter out duplicates...!
for (var i=0 ; i<Form1.ddlDistNamesFound.length;i++)
{
for (var j=i+1;j<Form1.ddlDistNamesFound.length;j++)
{
if (Form1.ddlDistNamesFound[i].value==Form1.ddlDistNamesFound[j].value)
{
Form1.ddlDistNamesFound.removeChild(Form1.ddlDistNamesFound[j])
}
}
}
[/code]


Which I would have thought would delete the duplicate, I think it's the right 'logic', but I don't think the removeChild command is correct.

Any ideas?

TIA
×

Success!

Help @_J_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...