2

I am using Angular 8. I want to check in Angular if object exists inside array of objects or not. My object is :

let objVenIns = {
                'CntNumber': 4,
                'CntMixer': 2,
                'DevicePositions': 'NA',
                'AddComments': 'NA',                
            }

Array of object contains other columns as well:-

let arrRowData = [0: {'SrNo' : 1, 'Name' : 'ABC', 'CntNumber': 1,'CntMixer': 3,'DevicePositions': 'Right','AddComments': 'NA'},
    1: {'SrNo' : 2, 'Name' : 'DEF', 'CntNumber': 4,'CntMixer': 2,'DevicePositions': 'NA','AddComments': 'NA'},
    2: {'SrNo' : 3, 'Name' : 'XYZ', 'CntNumber': 2,'CntMixer': 5,'DevicePositions': 'Left','AddComments': 'NA'}]

In Angular JS I used to perform following function to check if object exists in array or not:-

var data = $filter('filter')($scope.arrRowData, objVenIns, true)[0];

How to achieve such object search in Angular 8?

simple user
  • 349
  • 3
  • 22
  • 44

5 Answers5

4

You can use the same array.Find or Array.Filter with Angular as well.

let arrRowData = [{'SrNo' : 1, 'Name' : 'ABC', 'CntNumber': 1,'CntMixer': 3,'DevicePositions': 'Right','AddComments': 'NA'},
    {'SrNo' : 2, 'Name' : 'DEF', 'CntNumber': 4,'CntMixer': 2,'DevicePositions': 'NA','AddComments': 'NA'},
    {'SrNo' : 3, 'Name' : 'XYZ', 'CntNumber': 2,'CntMixer': 5,'DevicePositions': 'Left','AddComments': 'NA'}];

let objVenIns = {
                'CntNumber': 4,
                'CntMixer': 2,
                'DevicePositions': 'NA',
                'AddComments': 'NA',                
            };
            
//if you want to check whole object
var isInArray = arrRowData.indexOf(objVenIns) !== -1;
console.log(isInArray);

//if you want to check by prop values

var isInArray1 = arrRowData.find(function(el){ return el.CntNumber === 4 && el.CntMixer ==2 }) !== undefined;
console.log(isInArray1);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I have edited my post and changed array structure. My bad that I didnt post initially. I am looking for way to check whole object. Can you pleasehelp here? – simple user Nov 16 '19 at 07:21
2

You can identify object contains array or object using this Link StackBlitz Link here, You can do by your custom way using Array.isArray().

  • Array Structure is..

    arrRowData = [
        {'SrNo' : 1, 'Name' : 'ABC', 'CntNumber': 1,'CntMixer': 3,'DevicePositions': 'Right','AddComments': 'NA'},
        {'SrNo' : 2, 'Name' : 'DEF', 'CntNumber': 4,'CntMixer': 2,'DevicePositions': 'NA','AddComments': 'NA'},
        {'SrNo' : 3, 'Name' : 'XYZ', 'CntNumber': 2,'CntMixer': 5,'DevicePositions': 'Left','AddComments': 'NA'},
        [{'SrNo':4}]
     ]
    
  • Your method to check weather array contains object or array

    isObject(values){
       if (!Array.isArray (values) ){ return true;}
       else{ return false; }
    }
    
  • Your Template HTML is

    <div *ngFor= "let section of arrRowData ">
          <ul>
               <li *ngIf="isObject(section); else printVal">
                    <span> This is Object {{section |json}} </span>
               </li>
          </ul>
          <ng-template #printVal>
                <li>  This is Array {{section | json}} </li>
          </ng-template>  
    </div>
    
Developer
  • 3,309
  • 2
  • 19
  • 23
2

If you just want to check if the object exists, you could use Array#some:

const arrRowData = [
  {
    SrNo: 1,
    Name: 'ABC',
    CntNumber: 1,
    CntMixer: 3,
    DevicePositions: 'Right',
    AddComments: 'NA'
  },
  {
    SrNo: 2,
    Name: 'DEF',
    CntNumber: 4,
    CntMixer: 2,
    DevicePositions: 'NA',
    AddComments: 'NA'
  },
  {
    SrNo: 3,
    Name: 'XYZ',
    CntNumber: 2,
    CntMixer: 5,
    DevicePositions: 'Left',
    AddComments: 'NA'
  }
];
const objVenIns = {
  CntNumber: 4,
  CntMixer: 2,
  DevicePositions: 'NA',
  AddComments: 'NA'
};

const areObjectEquals = (obj1, obj2) =>
  obj1.CntMixer === obj2.CntMixer &&
  obj1.CntNumber === obj2.CntNumber &&
  obj1.DevicePositions === obj2.DevicePositions &&
  obj1.DevicePositions === obj2.DevicePositions &&
  obj1.AddComments === obj2.AddComments;

const objectExistsInArray = arrRowData.some(row => areObjectEquals(row, objVenIns));

console.log('objectExistsInArray?', objectExistsInArray);

PS1: I strongly recommend you to create an interface to type your array, object and methods.

PS2: For a more dynamic approach where you don't need to inform which properties you want to check and you don't need to care if its properties change, I'd recommend you to look at this answer.

developer033
  • 24,267
  • 8
  • 82
  • 108
2

If you want get all objects which exist, then you can filter it:

let objVenIns = {
'CntNumber': 4,
'CntMixer': 2,
'DevicePositions': 'NA',
'AddComments': 'NA',
};


let arrRowData = [
{'SrNo' : 1, 'Name' : 'ABC', 'CntNumber': 1,'CntMixer': 3,'DevicePositions': 'Right','AddComments': 'NA'},
{'SrNo' : 2, 'Name' : 'DEF', 'CntNumber': 4,'CntMixer': 2,'DevicePositions': 'NA','AddComments': 'NA'},
{'SrNo' : 3, 'Name' : 'XYZ', 'CntNumber': 2,'CntMixer': 5,'DevicePositions': 'Left','AddComments': 'NA'}
];

const result = arrRowData.filter(f=>
f.CntMixer === objVenIns.CntMixer &&
  f.CntNumber === objVenIns.CntNumber &&
  f.DevicePositions === objVenIns.DevicePositions &&
  f.DevicePositions === objVenIns.DevicePositions &&
  f.AddComments === objVenIns.AddComments
);

console.log(result);
console.log(result.length > 0 ? 'exists' : 'no');
StepUp
  • 36,391
  • 15
  • 88
  • 148
-1

You can also use Array.Propotype.reduce() like so:

const filter = arrRowData.reduce((a, e, i) => {
    if (
      e.CntMixer === objVenIns.CntMixer &&
      e.CntNumber === objVenIns.CntNumber &&
      e.DevicePositions === objVenIns.DevicePositions &&
      e.DevicePositions === objVenIns.DevicePositions &&
      e.AddComments === objVenIns.AddComments
    )
      a.push(e); return a;
}, []);  

console.log(filter)
AllJs
  • 1,760
  • 4
  • 27
  • 48