14

Using AngularJS, I'm creating an addTodo function in my app.

I seem to be having trouble implementing a way to check the uniqueness of the object being added to the array, and having it followed by additional actions.

So far I'm able to get the additional actions working, but not the initial check for uniqueness. How can I implement the check for uniqueness action and then have it followed by the additional actions?

The addTodo function I'm trying to create flows like this (Bold means not implemented):

  1. Check if todo is already in todos

    1a. If it does exisit, don't push, display alert

  2. Check if todo is not blank

    2a. If it is blank, don't push, display alert

  3. If unique and not blank, push to todos, display success message

Current addTodo function (without unqiueness check):

$scope.addTodo = function(){
  $scope.isVisible = true;
  if ($scope.todo) {
    $scope.todos.push($scope.todo);
    $scope.todo = '';
    $scope.alert = $scope.alerts[1];
  }else{
    $scope.alert = $scope.alerts[0];
  }
};

Note 1: $scope.alert and $scope.alerts are used to display certain error messages;

$scope.alerts[0]

"Please add text to your task."

$scope.alerts[1]

"Added a new task!"

The alert I want to display if the task being added already exists is

$scope.alerts[3] 

"Task already in list."

Note 2: $scope.isVisible toggles the visibility of the alert

4
  • So todo is a simple string or an object? Commented Aug 18, 2013 at 11:10
  • It's a simple string, yes Commented Aug 18, 2013 at 11:18
  • Hope you have checked my answer then. Commented Aug 18, 2013 at 11:26
  • I have, thank you! Adding a comment to it. Commented Aug 18, 2013 at 11:35

3 Answers 3

21

use Array.indexOf this way:

$scope.addTodo = function(){
  $scope.isVisible = true;
  if ($scope.todo) {
    if ($scope.todos.indexOf($scope.todo) == -1) {
        $scope.todos.push($scope.todo);
        $scope.todo = '';
        $scope.alert = $scope.alerts[1];
    }else{
     // $scope.todo is already in the $scope.todos array, alert the user
        $scope.alert = $scope.alerts[3];
    }
  }else{
    $scope.alert = $scope.alerts[0];
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic, thank you! Solved my main issues of not being sure where in the flow to implement the action and how to properly implement it based on what I already had.
Note for others: mef's flow differs from the flow I established above, but is actually the correct way to look at it: First, check if the todo is blank. If it isn't, THEN check if it is unique. My pitfall in the code below was I was too attached to the idea that I needed to check if the todo was unique first before checking if it was blank.
2

if you are using underscore OR lodash, you can use this simple code to push unique items in array.

if (_.findWhere($scope.todos, $scope.todo) == null) {
    $scope.todos.push($scope.todo);
}

Hope this helps!!!

Comments

0

If you are using a standard string todo you can use the javascript array indexOf method. This method for object data types does a reference match.

Else you can also look at jquery grep method.

Use one of these methods to check if $scope.todos already contains such element and show the alert.

1 Comment

Based on that, I've edited the code like this, but it's still allowing duplicates to be pushed to the array; $scope.addTodo = function(){ $scope.isVisible = true; var todos = $scope.todos; var todo = $scope.todo; if (todo == todos.indexOf(todo)){ $scope.alert = $scope.alerts[3]; } else if ($scope.todo) { $scope.todos.push($scope.todo); $scope.todo = ''; $scope.alert = $scope.alerts[1]; } else { $scope.alert = $scope.alerts[0]; } };

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.