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?
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');