'check for duplicates in multiple fields in array of objects based on condition in javascript
I have array of object in which return true or false based on duplicates
based on below conditions return true or false in javascript
if the if type is local has duplicate code value in object array return true
if the if type is IN or IN travel has duplicate code value in object array return false
if the if same type IN and has same code value in object array return true
if the if same type IN Travel and has same code value in object array 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
// this code not working for 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) {
return true;
} else {
return false;
}
if(getIN.length === checkINcode.length){
return true;
} else {
return false;
}
}
})
}
Solution 1:[1]
const filter = (arr) => {
for (let i = 0; i < arr.length; i++)
for (let j = 0; j < arr.length; j++)
if (i !== j && arr[i].code == arr[j].code)
return false;
return true;
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
