No se obtienen todos los objetos coincidentes, se devuelven todos los objetos excepto '{ID: 0, Nombre:' Grupo general '}', quiero todos los objetos coincidentes

 let arrDashboardIconGroups = [
      { ID:0, Name: 'General Group' },
      { ID: 1, Name: 'Patient Administration' },     
      { ID: 2, Name: 'Medical Charts' },
      { ID: 3, Name: 'Medical Procedures' },
      { ID: 13, Name: 'Purchase' },
      { ID: 14, Name: 'Sales' },
      { ID: 5, Name: 'Insurance' },
      { ID: 4, Name: 'Cash' },
      { ID: 6, Name: 'Pharmacy' },
      { ID: 7, Name: 'Inventory' },
      { ID: 8, Name: 'Lab' },
      { ID: 9, Name: 'Imaging' },
      { ID: 10, Name: 'In Patient' },
      { ID: 11, Name: 'System Administration' },
      { ID: 12, Name: 'Accounting' }
  ]  

const getMatchedobjects=()=> {

   let result = [0,1,2,3,4,]
   let matchedArray = arrDashboardIconGroups.filter(val =>result.find(
      (items)=>items == val.ID))

   console.log(matchedArray)
}

getMatchedobjects()
0
ASIF KAIF 13 jul. 2021 a las 10:04

3 respuestas

La mejor respuesta

Problema

Find devuelve un valor / objeto (la primera coincidencia), pero parece que desea filtrar la matriz arrDashboardIconGroups por aquellas que coinciden con una id especificada en la matriz result. Cuando se devuelve result[0], 0 es el valor, que es falso, y el filtro no devuelve el elemento de la matriz arrDashboardIconGroups.

Solución

Utilice Array.prototype.some para devolver el booleano que filter necesita para incluir un elemento en la matriz de resultados.

let arrDashboardIconGroups = [
  { ID:0, Name: 'General Group' },
  { ID: 1, Name: 'Patient Administration' },     
  { ID: 2, Name: 'Medical Charts' },
  { ID: 3, Name: 'Medical Procedures' },
  { ID: 13, Name: 'Purchase' },
  { ID: 14, Name: 'Sales' },
  { ID: 5, Name: 'Insurance' },
  { ID: 4, Name: 'Cash' },
  { ID: 6, Name: 'Pharmacy' },
  { ID: 7, Name: 'Inventory' },
  { ID: 8, Name: 'Lab' },
  { ID: 9, Name: 'Imaging' },
  { ID: 10, Name: 'In Patient' },
  { ID: 11, Name: 'System Administration' },
  { ID: 12, Name: 'Accounting' }
];

const getMatchedobjects = () => {
  let result = [0,1,2,3,4,];
  let matchedArray = arrDashboardIconGroups.filter(val => result.some((items)=>items == val.ID));

  console.log(matchedArray);
}

getMatchedobjects();

Alternativamente, también puede verificar que la matriz result incluya el id correspondiente.

let arrDashboardIconGroups = [
  { ID:0, Name: 'General Group' },
  { ID: 1, Name: 'Patient Administration' },     
  { ID: 2, Name: 'Medical Charts' },
  { ID: 3, Name: 'Medical Procedures' },
  { ID: 13, Name: 'Purchase' },
  { ID: 14, Name: 'Sales' },
  { ID: 5, Name: 'Insurance' },
  { ID: 4, Name: 'Cash' },
  { ID: 6, Name: 'Pharmacy' },
  { ID: 7, Name: 'Inventory' },
  { ID: 8, Name: 'Lab' },
  { ID: 9, Name: 'Imaging' },
  { ID: 10, Name: 'In Patient' },
  { ID: 11, Name: 'System Administration' },
  { ID: 12, Name: 'Accounting' }
]; 

const getMatchedobjects=()=> {
  let result = [0,1,2,3,4,];
  let matchedArray = arrDashboardIconGroups.filter(val => result.includes(val.ID)) ;

  console.log(matchedArray);
}

getMatchedobjects();
1
Drew Reese 13 jul. 2021 a las 07:17

Estás devolviendo el valor de

result.find((items) => items == val.ID)

En el primer caso, el valor devuelto es 0 que es un valor falsy. Por lo tanto, no se incluirá en el resultado del filtro final.

Puede ejecutar el siguiente código y ver los valores devueltos.

let arrDashboardIconGroups = [
  { ID: 0, Name: "General Group" },
  { ID: 1, Name: "Patient Administration" },
  { ID: 2, Name: "Medical Charts" },
  { ID: 3, Name: "Medical Procedures" },
  { ID: 13, Name: "Purchase" },
  { ID: 14, Name: "Sales" },
  { ID: 5, Name: "Insurance" },
  { ID: 4, Name: "Cash" },
  { ID: 6, Name: "Pharmacy" },
  { ID: 7, Name: "Inventory" },
  { ID: 8, Name: "Lab" },
  { ID: 9, Name: "Imaging" },
  { ID: 10, Name: "In Patient" },
  { ID: 11, Name: "System Administration" },
  { ID: 12, Name: "Accounting" },
];

const getMatchedobjects = () => {
  let result = [0, 1, 2, 3, 4];
  let matchedArray = arrDashboardIconGroups.filter((val) => {
    const returnResult = result.find((items) => items == val.ID);
    console.log(returnResult);
    return returnResult;
    // result.includes(val.ID)
  });

  console.log(matchedArray);
};

getMatchedobjects();

Alternativamente, puede utilizar incluye

let arrDashboardIconGroups = [
  { ID: 0, Name: "General Group" },
  { ID: 1, Name: "Patient Administration" },
  { ID: 2, Name: "Medical Charts" },
  { ID: 3, Name: "Medical Procedures" },
  { ID: 13, Name: "Purchase" },
  { ID: 14, Name: "Sales" },
  { ID: 5, Name: "Insurance" },
  { ID: 4, Name: "Cash" },
  { ID: 6, Name: "Pharmacy" },
  { ID: 7, Name: "Inventory" },
  { ID: 8, Name: "Lab" },
  { ID: 9, Name: "Imaging" },
  { ID: 10, Name: "In Patient" },
  { ID: 11, Name: "System Administration" },
  { ID: 12, Name: "Accounting" },
];

const getMatchedobjects = () => {
  let result = [0, 1, 2, 3, 4];
  let matchedArray = arrDashboardIconGroups.filter((val) => result.includes(val.ID));

  console.log(matchedArray);
};

getMatchedobjects();
2
decpk 13 jul. 2021 a las 07:17

find() devuelve el valor del primer elemento en la matriz proporcionada que satisface la función de prueba proporcionada. Dado que 0 es un valor falso, no lo incluye en la matriz final. Ver MDN Docs

Puede map la matriz result y usar la función arrDashboardIconGroups find y devolver los objetos coincidentes.

let arrDashboardIconGroups = [
      { ID: 0, Name: 'General Group' },
      { ID: 1, Name: 'Patient Administration' },     
      { ID: 2, Name: 'Medical Charts' },
      { ID: 3, Name: 'Medical Procedures' },
      { ID: 13, Name: 'Purchase' },
      { ID: 14, Name: 'Sales' },
      { ID: 5, Name: 'Insurance' },
      { ID: 4, Name: 'Cash' },
      { ID: 6, Name: 'Pharmacy' },
      { ID: 7, Name: 'Inventory' },
      { ID: 8, Name: 'Lab' },
      { ID: 9, Name: 'Imaging' },
      { ID: 10, Name: 'In Patient' },
      { ID: 11, Name: 'System Administration' },
      { ID: 12, Name: 'Accounting' }
  ]  

const getMatchedobjects=()=> {

   let result = [0, 1,2,3,4,99]   //99 for trial
   let matchedArray = result.map((res)=>arrDashboardIconGroups.find((val)=>val.ID == res)).filter(Boolean);

   console.log(matchedArray)
}

getMatchedobjects()
0
TechySharnav 14 jul. 2021 a las 08:29