I have a jQuery plugin, which I created with partial help looking at this thread: jQuery.unique on an array of strings.
Here is my code:
// function
$(function() {
$.fn.removeDuplicates = function() {
var arrResult = [];
for (var i = 0, n = this.length; i < n; i++) {
var item = this[i];
var index = item.SerialNo + " - " + item.UPC + " - " + item.Name + " - " + item.Version + " - " + item.Description;
arrResult[ index ] = item;
}
var i = 0;
var nonDuplicatedArray = [];
for (var item in arrResult) {
nonDuplicatedArray[i++] = arrResult[item];
}
return (this = nonDuplicatedArray);
};
});
var arrObj = [
{ "SerialNo:"1234", "UPC":"ABCXYZ", "Name":"Test", "Version":"1", "Description":"test"},
{ "SerialNo:"1234", "UPC":"ABCXYZ", "Name":"Test", "Version":"1", "Description":"test"},
{ "SerialNo:"12345", "UPC":"ABCXYZ123", "Name":"Test2", "Version":"2", "Description":"test2"}
];
$(arrObj).removeDuplicates();
I want to modify this
and return it. The plugin doesn't have to be chained, but I want to keep the function call the same. I realize I can modify the function by returning nonDuplicatedArray
and set it to a variable in the function call, ergo:
var newVar = $(arrObj).removeDuplicates();
But I was hoping there's a way to directly modify this
inside the function and return this
, while preserving the original function call. Simply, how do I set nonDuplicatedArray
to this
?
Thank you.