• Skip to primary navigation
  • Skip to main content
  • Skip to footer
WittySparks Logo White

WittySparks

Ignite Your Thoughts

  • Topics
  • Reviews
  • Subscribe
WittySparks / Technology / Programming / Avoid empty and duplicate records to get pushed in AngularJS
AngularJS Framework from Google

Avoid empty and duplicate records to get pushed in AngularJS

Programming June 22, 2013 by Sravan Kumar Kandagatla

I am back again with a basic example on AngularJS on how to display the data from a set or array or JSON array, and how to add or insert or append data in an existing JSON array, and at the same time, how could we avoid empty data or duplicate records to get pushed into the JSON array.

This time I am back with Screencast Video tutorials or presentation whatever you call it is, and this is my first screen-cast video presentation too and that too in AngularJS; going forward, I will try to improve my voice and presentation skills, which will help me and you to get some online tutorials going forward.

Since this is my first video, I expect your valuable feedback on where I have to improve myself – which will help me grow further for a better presentation next time. I appreciate your feedback.

Ok – let me come back to this basic AngularJS tutorial; in this tutorial, you can see how to use the ngRepeat directive, forEach global API, and, while inserting data, how to avoid duplicate or empty data to get a push into the JSON array.

Here is the screen-cast video, which explains you well in detail in case you are a starter in AngularJS or about to learn AngularJS:

AngularJS – Basic example using ngRepeat and forEach

Demo | Download

JavaScript Code:

          var wittyApp = angular.module('wittyApp',[]);
          function AvoidDuplicateEntries($scope){
          $scope.movies = [
          {moviename: 'SuperMan'},
          {moviename: 'SpiderMan'},
          {moviename: 'BatMan'}
          ];
          var someMovie = $scope.movies;
          $scope.addMovie = function (){
          var newMovie = $scope.moviename;
          var oldmovies;
          if(newMovie){ //This will avoid empty data
          angular.forEach($scope.movies, function(eachmovie){ //For loop
          if(newMovie.toLowerCase() == eachmovie.moviename.toLowerCase()){ // this line will check whether the data is existing or not
          oldmovies = true;
          }
          });
          if(!oldmovies){
          someMovie.push({moviename:newMovie});
          }
          }
          }
          }

HTML Code:

<div style="padding:20px" ng-app="wittyApp" id="ng-app" ng-controller="AvoidDuplicateEntries">
<h1>Basic App using AngularJS</h1>
<table class="table table-bordered" style="width:220px;">
<thead>
<tr><td>#</td>
<td>Movie Name</td></tr>
</thead>
<tr ng-repeat='movie in movies'>
<td>{{$index+1}}</td>
<td>{{movie.moviename}}</td>
</tr>
</table>
<form ng-submit="addMovie();">
<input type="text" ng-model="moviename" size="30" placeholder="Which movie you want to watch" /><br />
<input type="submit" class="btn btn-primary" value="Add Movie">
</form>
</div>

Liked it? Appreciate your feedback about my first video and first example on AngularJS :)
More to come – keep visiting us!

Appreciate your opinion on this topic on social media by tagging us @wittysparks
  • LinkedIn
  • Twitter
  • Like
  • Pinterest

Related Topics

  • Top JavaScript trends for 2020
  • Upgradation to AngularJS 7: Summary and Features
  • Why NodeJS Development is Important Nowadays
Launch Offer
Rocket.net - Managed WordPress hosting
Rocket.net - Managed WordPress hosting
$1 for first month, $25/month

A highly secured WordPress hosting platform powered by Cloudflare enterprises with customized services for agencies, eCommerce, and small businesses.

  • PageSpeed Guarantee
  • Ultra Secure Platform
  • FREE Migrations
  • Automatic Updates
Try for $1.00 Our Review
We earn a commission if you make a purchase, at no additional cost to you.

Our Favorite Tools

Semrush - SEO and Marketing Tools
Semrush - SEO and Marketing Tools
$119.95/mo
Try for FREE Our Review
Semrush - SEO and Marketing Tools
Lasso - All-in-one Affiliate Marketing Plugin for WordPress
$29/month
Start Free Trial Our Review
Semrush - SEO and Marketing Tools
Grammarly - AI-powered writing assistant
$12.00 / month
Try for FREE Our Review
Previous Post: « Dots a simple and interesting game for iOS
Next Post: HP All in one Slate 21 AIO, 21.5-inch android Tablet »

Reader Interactions

Profile picture for Anonymous

About Sravan Kumar Kandagatla

Founder of WittySparks
WittySparks Staff

Sravan is a highly experienced web designer, user experience designer and developer. You can connect with him on Twitter or LinkedIn and initiate a chat.

View all posts by Sravan Kumar Kandagatla
  • LinkedIN
  • Twitter
  • Pinterest
  • Facebook
  • Instagram

Comments

  1. Profile picture for GuestGuest says

    September 11, 2013 at 7:54 pm

    Might be a cool article but I wouldn’t know, I left before reading because there were too many spammy autoplay videos (with audio ON)…ads are one thing, but this is overkill man.

  2. Profile picture for Prashobh PSPrashobh PS says

    May 23, 2014 at 11:49 am

    You can simply use

    $scope.movies.indexOf(yourterm) == -1 {
    push data
    }

    • Profile picture for Sravan KumarSravan Kumar says

      May 25, 2014 at 9:07 am

      This is more optimized way of doing it – thanks for sharing the tip!

Footer

Search

Exclusive Coupons

  • Moqups Review - Use coupon code “WITTYSPARKS" for 20% off or "PARTNERS50" for 50% discount on all plans.
  • Serpstat Review - Use coupon code "wittysparks_discount" for 30% off.
  • WPForms Review - Use coupon code "WITTYSPARKS" for 50% off.
  • WPPayForm Review - Use coupon code "wittysparks" for 10% to 40% off.

Affiliate Disclosure

If you make a purchase from WittySparks links, we will receive a small commission. See our Affiliate Disclosure.

Sponsors

Partnered with FreePik to use the licensed images.

turn to dhgate for smartphone

Follow Us

  • Facebook
  • Twitter
  • Pinterest
  • LinkedIn
  • Instagram
  • YouTube
  • RSS

Copyright © 2022 · Hosting sponsored by Rocket.net (Affiliate link)

  • About Us
  • Contact Us
  • Privacy Policy
  • Affiliate Disclosure