Tengo una matriz de objetos arrayobj en la que tengo que verificar el valor de propiedad de
code y type según la declaración de devolución de la condición no add o can add en javascript
en arrobj for type-local, if type local and code values not reappearing in other object , se devuelve ' puede agregar '; de lo contrario, se devuelve 'sin agregar'
en arrobj for type-FL or FL Travel, if different type and code values reappearing in other object , se devuelve ' puede agregar '
en arrobj for type-FL or FL Travel if type and code both reappearing in object devuelve ' no add '
Cómo verificar la condición anterior en javascript,
probé el código sin verificar el type de propiedad, se atascó
function checkValues(arrob){ const checkcode = arrob.map(elem => elem.code); if (arrob.length !== new Set(checkcode).size) { return "no add"; } else { return "can add" } } checkValues(arrobj1); checkValues(arrobj2); checkValues(arrobj3); sample objects for scenario var arrobj1=[ {id:1, name: "ram", code: "NIXZ", type: "Local"}, {id:2, name: "abey", code: "ABCN", type: "FI"}, {id:3, name: "jaz", code: "ABCN", type: "FI Travel"} ] var arrobj2=[ {id:1, name: "ram", code: "NIXZ", type: "Local"}, {id:4, name: "zain", code: "NIXZ", type: "FI"}, {id:2, name: "abey", code: "ABCN", type: "FI"}, {id:3, name: "jaz", code: "ABCNE", type: "FI Travel"} ] var arrobj3=[ {id:1, name: "ram", code: "NIXZ", type: "Local"}, {id:4, name: "sam", code: "ABCN", type: "FI"}, {id:2, name: "abey", code: "ABCN", type: "FI"}, {id:3, name: "jaz", code: "ABCNE", type: "FI Travel"} ]Resultado Esperado
//for arrobj1 [type-FL or FL Travel, different type and same code appears] can add //for arrobj2 [type local, same code appears] no add //for arrobj3 [type-FL or FL Travel, same type and same code appears] no addEl método map() crea una nueva matriz con los resultados de llamar a una función proporcionada en cada elemento de la matriz de llamadas.
const rawArray = [arrobj1 , arrobj2 , arrobj3] const hasDups = (objArray) => { objArray.ForEach(el => { if (objArray.Includes(el.type)) return true; return false; }); } const newArray = rawArray.map(el => !hasDups(el));