Home > Back-end >  How can I match items in my array with items in my object?
How can I match items in my array with items in my object?

Time:06-25

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.

CodePudding user response:

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);

CodePudding user response:

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'
    }
  ]
]
  • Related