0

how to use unique filter in angularjs

$scope.students = [  
   { name: 'Hai', createdBy:'Thomas', createdOn:'09-09-2016' },  
   { name: 'Hal', createdBy:'Thomas', createdOn:'08-09-2016' }, 
   { name: 'Ho', createdBy:'Bob', createdOn:'07-09-2016', modifiedBy:'Alice', modifiedOn:'07-09-2016' },
   { name: 'Hoan', createdBy:'Thomas1', createdOn:'09-09-2016' }, 
   { name: 'Hieu', createdBy:'Bob', createdOn:'10-09-2016', modifiedBy:'Thomas1', modifiedOn:'07-09-2016' },
 ];  

I dont want Bob beacuse Bob creation is modified by Alice in student name 'Ho' and Hieu is modified by 'Thomas1'

my expected final output is in unique name of last updatedby

Bob is not eligible for this list ok...

Last updatedBy Last updated date 

------------    --------------

Thomas      09-09-2016

Alice       07-09-2016

Thomas1     09-09-2016
6
  • how do you defined unique here? Commented Jan 30, 2017 at 13:05
  • @Sajeetharan yes Ji i am struggling in this field I need a log report for last modifeid if modifiedBy is undefined means look createdBy any possiblity for this scenarion for getting this result Commented Jan 30, 2017 at 13:10
  • what do you want to do?and what have you done? Commented Jan 30, 2017 at 13:13
  • @jitendravarshney whether Its not posibble ji. Thats why I am asking you understood my scenario? Commented Jan 30, 2017 at 13:27
  • Do you want to make that with angular filters? Commented Jan 30, 2017 at 13:45

2 Answers 2

1

Try this:

app.filter('test', function() {
  return function(items) {
    var filtered = [];

    angular.forEach(items, function(el) {
      if(el.modifiedBy && el.modifiedOn) {
        el.createdBy = el.modifiedBy;
        el.createdOn = el.modifiedOn;
      }
    });
    for(var i = 1; i < items.length; ){
      if(items[i-1].createdBy == items[i].createdBy){
        items.splice(i, 1);
      } else {
        i++;
      }
    }
    return items;
  }
});

JsFiddle link here

Sign up to request clarification or add additional context in comments.

Comments

0

Rephrasing your problem statement into technical terms :

If a record is modified, then the modifier becomes the creator. How do I find the unique creators ?

One of the solution would be :

var students = [  
   { name: 'Hai', createdBy:'Thomas', createdOn:'09-09-2016' },  
   { name: 'Hal', createdBy:'Thomas', createdOn:'08-09-2016' }, 
   { name: 'Ho', createdBy:'Bob', createdOn:'07-09-2016', modifiedBy:'Alice', modifiedOn:'07-09-2016' },
   { name: 'Hoan', createdBy:'Thomas1', createdOn:'09-09-2016' }, 
   { name: 'Hieu', createdBy:'Bob', createdOn:'10-09-2016', modifiedBy:'Thomas1', modifiedOn:'07-09-2016' },
 ]; 

var studentsTemp = JSON.parse(JSON.stringify(students));  //deep copy

var unique = {};
 for(var i = 0; i < studentsTemp.length; i ++){
    if(studentsTemp[i].modifiedBy != null){
        studentsTemp[i].createdBy = studentsTemp[i].modifiedBy;
    }
    unique[studentsTemp[i].createdBy] = studentsTemp[i].createdOn;
 }

 console.log(unique);

Convert this code into an AngularJS filter.

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.