Given an array of objects, determine if there are any duplicates based on certain conditions in JavaScript.
The following conditions need to be checked:
- If the object with type 'local' has a duplicate code, return true
- If the object with type 'IN' or 'IN Travel' has a duplicate code, return false
- If there are objects with the same type 'IN' and the same code, return true
- If there are objects with the same type 'IN Travel' and the same code, return true
var objarray1=[
{id:1, name: "ram", code: "NIXZ", type: "Local"},
{id:2, name: "abey", code: "ABCN", type: "IN"},
{id:3, name: "jaz", code: "ABCN", type: "IN Travel"}
]
Expected Result
// since type-IN or IN travel and code has duplicate
false
// The existing code does not fulfil the above conditions
function checkArrayObject(arrobj){
const getLocal = arrobj.filter(e=>e.type=="local");
const checklocalcode = arrobj.filter(e=>e.type=="local").map(i=>i.code);
const getIN = arrobj.filter(e=>e.type=="IN");
const checkINcode = arrobj.filter(e=>e.type=="IN").map(i=>i.code);
var result;
arrobj.forEach(e=>{
if(getLocal.length === checklocalcode.length) {
result = true;
} else {
result = false;
}
if(getIN.length === checkINcode.length){
result = true;
} else {
result = false;
}
});
return result;
}