Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
How can I match items in my array with items in my object?

Take the following data structure

report = {
   fraud_ids: [2, 3, 4, 15],
}

 fraudTypes: {
      ACH: {
        fraud_category: "fraud",
        fraud_subcategory: "a",
        fraud_type_id: "4",
      },
      'Account takeover': {
        fraud_category: "misc",
        fraud_subcategory: "a",
        fraud_type_id: "2",
      },
      'Advance fee': { 
        fraud_category: "fraud",
        fraud_subcategory: "b",
        fraud_type_id: "8",
      },
      'Against Financial Institution Customer(s)': {
        fraud_category: "cyber",
        fraud_subcategory: "b",
        fraud_type_id: "15",
      },
      'Against Financial Institution(s)': {
        fraud_category: "cyber",
        fraud_subcategory: "a",
        fraud_type_id: "78",
      },
      'Alters or cancels transaction to avoid BSA recordkeeping requirement': {
        fraud_category: "structuring",
        fraud_subcategory: "a",
        fraud_type_id: "3",
      },
   }

I want to match the fraudTypes object with correct number in the array by fraud_type_id. And when I find a match I want to just return the key of the object.

So using the example above I would return ['Account takeover', 'Alters or cancels transaction to avoid BSA recordkeeping requirement', 'ACH', Against Financial Institution Customer(s)']

I wrote the following logic and to my surprise I get an array of four items that return undefined.

const x = report.fraud_ids.map(id => {
  Object.keys(fraudTypes).map(fraudDescription => {
    if (parseInt(fraudTypes[fraudDescription].fraud_type_id, 10) === id) {
      return true;
    }
  });
});

I expected this to return the matching four objects and then I know I have to write additional logic just to return the keys. What am I doing wrong?

Please see the codeSandbox.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You aren't returning anything from the first callback, nor is .map the correct inner method - instead use .find to find the key with the matching ID.

report={fraud_ids:[2,3,4,15],fraudTypes:{ACH:{fraud_category:"fraud",fraud_subcategory:"a",fraud_type_id:"4"},"Account takeover":{fraud_category:"misc",fraud_subcategory:"a",fraud_type_id:"2"},"Advance fee":{fraud_category:"fraud",fraud_subcategory:"b",fraud_type_id:"8"},"Against Financial Institution Customer(s)":{fraud_category:"cyber",fraud_subcategory:"b",fraud_type_id:"15"},"Against Financial Institution(s)":{fraud_category:"cyber",fraud_subcategory:"a",fraud_type_id:"78"},"Alters or cancels transaction to avoid BSA recordkeeping requirement":{fraud_category:"structuring",fraud_subcategory:"a",fraud_type_id:"3"}}};

const x = report.fraud_ids.map(id => (
  Object.keys(report.fraudTypes).find(fraudDescription =>
    Number(report.fraudTypes[fraudDescription].fraud_type_id) === id)
));
console.log(x);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I don't know if this is what you want, but there it goes.

const selectedFrauds = report.fraud_ids.map((id, index) => {
  return(
    Object.values(fraudTypes).map((fraud, idx) => {
      return id === +fraud.fraud_type_id ? fraud : "";
    })
  )
})

const cleanSelectedFrauds = selectedFrauds.map((el) => {
  return el.filter((innerEl) => {
    if(innerEl) {
      return innerEl
    }
  })
});


console.log(cleanSelectedFrauds)

This will return the following array:

[
  [
    {
      fraud_category: 'misc',
      fraud_subcategory: 'a',
      fraud_type_id: '2'
    }
  ],
  [
    {
      fraud_category: 'structuring',
      fraud_subcategory: 'a',
      fraud_type_id: '3'
    }
  ],
  [
    {
      fraud_category: 'fraud',
      fraud_subcategory: 'a',
      fraud_type_id: '4'
    }
  ],
  [
    {
      fraud_category: 'cyber',
      fraud_subcategory: 'b',
      fraud_type_id: '15'
    }
  ]
]
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda