1

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;
}
3
  • Did you include the librairies that you use ? Commented Jul 13, 2015 at 12:36
  • yes, everything is added Commented Jul 13, 2015 at 12:38
  • 2
    Use angular.forEach instead of $. Otherwise include jQuery libray. Commented Jul 13, 2015 at 12:38

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.