/    Sign up×
Community /Pin to ProfileBookmark

Array.prototype.unique? what does this do?

What is this syntax: Array.prototype.unique

What does it mean? What does it do?

Also, can someone explain what the following code does?

[CODE]
Array.prototype.unique = function () {
var r = new Array();
o:for(var i = 0, n = this.length; i < n; i++)
{
for(var x = 0, y = r.length; x < y; x++)
{
if(r[x]==this[i].category)
{
continue o;
}
}
r[r.length] = this[i].category;
}
return r;
}
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 28.2010 — It appears to a method that you would call from an object to return an array of that object's members category properties, if any.

The object has to have a length property, so it may be an array, a node list, an arguments object or any custom made object with a length and category.
Copy linkTweet thisAlerts:
@developer2000authorMar 28.2010 — Thank you for your reply but I don't understand how it will be used.


Here is the entire code in a .js file:

[CODE]
// call over to Alfresco and fetch the todo list
var connector = remote.connect("alfresco");
var data = connector.get("/todo");

// create json object from data
var results = eval('(' + data + ')');

model.tasks = results["tasks"];

// do array unique thingy
var catArray = results["categories"];

Array.prototype.unique = function () {
var r = new Array();
o:for(var i = 0, n = this.length; i < n; i++)
{
for(var x = 0, y = r.length; x < y; x++)
{
if(r[x]==this[i].category)
{
continue o;
}
}
r[r.length] = this[i].category;
}
return r;
}
if (catArray) {
var unique = catArray.unique();
model.cats = unique;
} else {
model.cats = [];
}
[/CODE]
Copy linkTweet thisAlerts:
@rnd_meMar 28.2010 — [CODE]Array.prototype.unique=function(){
var i=0, mx=this.length, last, o=[], r=this.concat().sort();
for(i;i<mx;i++){ if(last!==r[i]){last=o[o.length]=r[i];} }
return o;
}



array=[5,2,5,8,5,3,4,6,6,2,5,2];
alert(array.unique()); //shows:2,3,4,5,6,8[/CODE]
Copy linkTweet thisAlerts:
@developer2000authorMar 29.2010 — Thanks for the example, but I'm getting the error:

Caused by: org.mozilla.javascript.EvaluatorException: Cannot modify a property of a sealed object: unique.

would you know what is causing the error or how I can work around it?


Caused by: org.mozilla.javascript.EvaluatorException: Cannot modify a property of a sealed object: unique. (file:C:/Programs/Alfresco/AlfrescoEE3.2r_test_todolist/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/components/dashlets/todo/todolist.get.js#14)

at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultError

Reporter.java:109)

at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1030)

at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1086)

at org.mozilla.javascript.Context.reportRuntimeError1(Context.java:1049)

at org.mozilla.javascript.ScriptableObject.checkNotSealed(ScriptableObje
ct.java:1524)

at org.mozilla.javascript.ScriptableObject.putImpl(ScriptableObject.java

:1991)

at org.mozilla.javascript.ScriptableObject.put(ScriptableObject.java:312

)

at org.mozilla.javascript.IdScriptableObject.put(IdScriptableObject.java

:419)

at org.mozilla.javascript.NativeArray.put(NativeArray.java:288)

at org.mozilla.javascript.ScriptableObject.putProperty(ScriptableObject.

java:1656)

at org.mozilla.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java

:1477)

at org.mozilla.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java

:1467)

at org.mozilla.javascript.gen.c5._c0(file:C:/Programs/Alfresco/AlfrescoE

E3.2r_test_todolist/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscript

s/org/alfresco/components/dashlets/todo/todolist.get.js:14)

at org.mozilla.javascript.gen.c5.call(file:C:/Programs/Alfresco/Alfresco

EE3.2r_test_todolist/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscrip

ts/org/alfresco/components/dashlets/todo/todolist.get.js)

at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:3

93)

at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:283

4)

at org.mozilla.javascript.gen.c5.call(file:C:/Programs/Alfresco/Alfresco

EE3.2r_test_todolist/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscrip

ts/org/alfresco/components/dashlets/todo/todolist.get.js)

at org.mozilla.javascript.gen.c5.exec(file:C:/Programs/Alfresco/Alfresco

EE3.2r_test_todolist/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscrip

ts/org/alfresco/components/dashlets/todo/todolist.get.js)

at org.alfresco.web.scripts.PresentationScriptProcessor.executeScriptImp

l(PresentationScriptProcessor.java:314)

... 109 more
×

Success!

Help @developer2000 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...