简体   繁体   English

如何将数组中的项目与对象中的项目匹配?

[英]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.我想通过欺诈类型 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)']因此,使用上面的示例,我将返回['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 .请参阅 代码沙盒

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.您没有从第一个回调中返回任何内容, .map也不是正确的内部方法 - 而是使用.find来查找具有匹配 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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的循环没有分隔与另一个数组不匹配的数组项,我该怎么做? - My loop isn't separating the array items that don't match on the other array, how can I do that? 为什么我不能将我的项目分配给对象的常量? - Why can I not assign my items to my object's constant? 如何只保留符合特定条件的数组项? - How can I only keep items of an array that match a certain condition? 如何在不制作 JS object 的情况下将商品添加到购物车 - How can I add items to my cart without making a JS object 在 React 中,如何使用复选框创建一个灵活的选定项目数组,类似于在我的手机上选择图像? - In React, how can I create a flexible array of selected items, using checkboxes, similar to selecting images on my Phone? 如何在Riot.js组件的html部分中设置项目数组? - How can I set an array of items in the html part of my Riot.js component? 如何在返回数组时一次渲染一个项目? - How can I render out the items in my array one at a time when returning them? ReactJS,Redux:如何在STATE中返回一组项目? - ReactJS, Redux : How do I return an array of items in my STATE? 如何在 ReactJS 中按 object arrays 过滤数组项? - How can I filter array items by object arrays in ReactJS? 如何在列表中有效地弯曲我的项目? - How can I effectively curve my items inside a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM