0

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.

Community
  • 1
  • 1
user717236
  • 4,959
  • 19
  • 66
  • 102
  • 1
    It doesn't really seem like the kind of thing you'd use a jQuery plugin for, passing an array as the selector etc ? – adeneo Jan 24 '14 at 17:22
  • I'd love to be educated. Why wouldn't I use a jQuery plugin in this case? I'm being sincere. Maybe you're right. – user717236 Jan 24 '14 at 17:40
  • 1
    You're changing an array, something you could just use a function for. jQuery is usually used for manipulating the DOM, and passing an array to have a jQuery plugin that just changes the array and returns it seems like a bad use of jQuery as it would be even easier to just use a regular function. – adeneo Jan 24 '14 at 17:45
  • Yeah, I think what you're saying makes a lot of sense. What you're saying I would better off doing something like creating an array prototype function, instead of using a JS framework. – user717236 Jan 24 '14 at 17:50
  • 1
    Yes, that's what I'm saying, and at the same time prototyping on native objects like Array is not always a good idea either, but in this case it seems like a much better idea than using jQuery. – adeneo Jan 24 '14 at 18:00
  • Good to know. I'll take that to heart. Thank you for the knowledge transfer. – user717236 Jan 24 '14 at 18:01

1 Answers1

1

At the end, just return the array, there's no need to set this to nonDuplicatedArray once the function has done what it's supposed to do, you no longer need the this keyword, you just need to return the array ?

return nonDuplicatedArray;

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Ah. So, the original variable, `arrObj`, will be modified by returning `nonDuplicatedArray`? I don't have to set it to something like `newVar`? Actually, according to the fiddle, the return is setting to a new variable. What I want to do is not have to do that. – user717236 Jan 24 '14 at 17:35
  • 1
    @user717236 - If you're going to chain it, you'd have to wrap it, as in `return $(nonDuplicatedArray);` but then you have a jQuery object containing the array. I returned the array somewhere to console log it, but you don't have to do that. Here's an example -> http://jsfiddle.net/jZ46P/1/ – adeneo Jan 24 '14 at 17:41
  • 1
    Or without chaining, and just returning the array -> http://jsfiddle.net/jZ46P/2/ – adeneo Jan 24 '14 at 17:42
  • 1
    Whatever you return is now the value. – adeneo Jan 24 '14 at 17:42
  • Oh OK. I see now. That makes perfect sense. Thank you for your help and the education! – user717236 Jan 24 '14 at 17:47