ahmad
ahmad

Reputation: 55

Add distinct items to an array in angularjs

I'm trying to add to an array distinct item but this error appears "ReferenceError: $ is not defined" any help

function unique(list) {
  var result = [];
  $.each(list, function(i, e) {
    if ($.inArray(e, result) == -1) result.push(e);
  });
  return result;
}

Upvotes: 1

Views: 504

Answers (2)

LZL
LZL

Reputation: 3

If you did load JQuery in your, have you defined it as $?

You can use Array.forEach and

Array.indexOf to solve your problem.

SO does not allow me for more that two links. but you can use the ES6 array.find function to do the same job

Upvotes: 0

borracciaBlu
borracciaBlu

Reputation: 4225

It seems you didn't load jQuery.
Btw to implement this function you don't need that.

Try this:

function unique(list) {
    var result = [];

    for (var i = list.length - 1; i >= 0; i--) {
        if (result.indexOf(list[i]) == -1) {
            result.push(list[i]);
        }
    };

    return result;
}

Upvotes: 1

Related Questions