如何插入数组或值的对象并传输到新数据中

时间:2019-06-18 05:16:03

标签: javascript arrays react-native lodash

我有两个数据,分别称为 记录 联系人 ,它们都有不同的 lead_id 。 如果 lead_id的 相同,则此对象表示它们将连接或插入并转换为新的 newRecords

我制作了一个示例数据,将其作为示例。我希望你能举个例子。

"records": [
  {
    "lead_id": 95173,
    "user_id": 526,
    "first_name": "Aaron",
    "last_name": "De La Rosa",
  },
  {
    "lead_id": 95972,
    "user_id": 459,
    "first_name": "Abba",
    "last_name": "Lorenzo",
  },
  {
    "lead_id": 95974,
    "user_id": 459,
    "first_name": "Abba",
    "last_name": "Lorenzo",
  }
]
"contacts": [
  {
    "lead_id": 95173,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 25,
    "lead_contact_number": "+1 206-501-4581"
  },
  {
    "lead_id": 95972,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 26,
    "lead_contact_number": "+1 206-501-4582"
  },
  {
    "lead_id": 95974,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 27,
    "lead_contact_number": "+1 206-501-4583"
  },
  {
    "lead_id": 95173,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 28,
    "lead_contact_number": "+1 206-501-4584"
  }
]

预期产量

"newRecords": [
    {
      "lead_id": 95173,
      "user_id": 526,
      "first_name": "Aaron",
      "last_name": "De La Rosa",
      "contact_details": [
        {
          "lead_id": 95173,
          "lead_contact_number_type_id": 1,
          "lead_contact_number_id": 25,
          "lead_contact_number": "+1 206-501-4581"
        },
        {
          "lead_id": 95173,
          "lead_contact_number_type_id": 1,
          "lead_contact_number_id": 28,
          "lead_contact_number": "+1 206-501-4584"
        }
      ]
    },
    {
      "lead_id": 95972,
      "user_id": 459,
      "first_name": "Abba",
      "last_name": "Lorenzo",
      "contact_details": [
        {
          "lead_id": 95972,
          "lead_contact_number_type_id": 1,
          "lead_contact_number_id": 26,
          "lead_contact_number": "+1 206-501-4582"
        }
      ]
    },
    {
      "lead_id": 95974,
      "user_id": 459,
      "first_name": "Abba",
      "last_name": "Lorenzo",
      "contact_details": [
        {
          "lead_id": 95974,
          "lead_contact_number_type_id": 1,
          "lead_contact_number_id": 27,
          "lead_contact_number": "+1 206-501-4583"
        }
      ]
    }
]

示例代码

let newRecordsMultContacts = newRecordsMult.map(records => {
    lead_id = records.lead_id;
    numbers = contacts
    .filter(number => number.lead_id === lead_id)
    .map(number => number.lead_contact_number);
   return { ...records, lead_contact_number: numbers, lead_contact_number_id: number.lead_contact_number_id, lead_contact_number_type_id: number.lead_contact_number_type_id }; 
})

4 个答案:

答案 0 :(得分:2)

您可以在此处应用mapfilter方法的组合。

应用map并添加通过匹配lead_id作为每个记录的新contact_details字段过滤的联系人。

下面的代码应该可以解决问题。

let newRecords = records.map((record) => {
  record['contact_details'] = contacts.filter((contact) => record.lead_id === contact.lead_id);
  return record;
});

答案 1 :(得分:1)

您可以采用的另一种方法是通过Array.reduce通过lead_id对联系人进行分组。之后,通过记录进行映射,并通过使用lead_id键访问联系人来构成最终对象。

let records = [{ "lead_id": 95173, "user_id": 526, "first_name": "Aaron", "last_name": "De La Rosa", }, { "lead_id": 95972, "user_id": 459, "first_name": "Abba", "last_name": "Lorenzo", }, { "lead_id": 95974, "user_id": 459, "first_name": "Abba", "last_name": "Lorenzo", } ]
let contacts = [{ "lead_id": 95173, "lead_contact_number_type_id": 1, "lead_contact_number_id": 25, "lead_contact_number": "+1 206-501-4581" }, { "lead_id": 95972, "lead_contact_number_type_id": 1, "lead_contact_number_id": 26, "lead_contact_number": "+1 206-501-4582" }, { "lead_id": 95974, "lead_contact_number_type_id": 1, "lead_contact_number_id": 27, "lead_contact_number": "+1 206-501-4583" }, { "lead_id": 95173, "lead_contact_number_type_id": 1, "lead_contact_number_id": 28, "lead_contact_number": "+1 206-501-4584" } ]

let cMap = contacts.reduce((r,c) => ((r[c.lead_id] = r[c.lead_id] || []).push(c), r), {})

let result = records.map(x => ({...x, contact_details: cMap[x.lead_id]}))

console.log(result)

该方法应具有很好的性能,因为您不必针对Contacts数组在每个迭代过滤器中都使用

答案 2 :(得分:1)

@MikeVictoria您的尝试是正确的,但有很小的变化。您可以通过使用map和filter来减少它,因此map through记录将为您提供相同长度的 records 数组的新副本,然后在迭代时返回所有属性,并向每个对象添加一个新属性。添加 contact_details 作为属性,并基于 lead_id 联系人进行过滤,这样它将过滤所有对象并提供一个新数组,该数组将设置为contact_details属性

我希望这能解决您的问题。如果我错过了任何事情,请告诉我

let records = [
  {
    "lead_id": 95173,
    "user_id": 526,
    "first_name": "Aaron",
    "last_name": "De La Rosa",
  },
  {
    "lead_id": 95972,
    "user_id": 459,
    "first_name": "Abba",
    "last_name": "Lorenzo",
  },
  {
    "lead_id": 95974,
    "user_id": 459,
    "first_name": "Abba",
    "last_name": "Lorenzo",
  }
]
let contacts = [
  {
    "lead_id": 95173,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 25,
    "lead_contact_number": "+1 206-501-4581"
  },
  {
    "lead_id": 95972,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 26,
    "lead_contact_number": "+1 206-501-4582"
  },
  {
    "lead_id": 95974,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 27,
    "lead_contact_number": "+1 206-501-4583"
  },
  {
    "lead_id": 95173,
    "lead_contact_number_type_id": 1,
    "lead_contact_number_id": 28,
    "lead_contact_number": "+1 206-501-4584"
  }
]


let expectedOutput = records.map((record) => {
  record['contact_details'] = contacts.filter((contact) => record.lead_id === contact.lead_id);
  return record;
});

console.log("expectedOutput", expectedOutput)

答案 3 :(得分:0)

我将通过lead_id(O(n)-n个联系人)生成一个连续字典,然后映射记录,并通过lead_id从映射中获取联系人(O (m)-m个记录数)总计为O(n + m)。

在地图中使用过滤器的条件是O(m * n),因为您每次都需要过滤整个联系人数组。

我使用_.overArgs()生成了接受recordscontacts的函数。该函数使用lead_id_.groupBy()创建联系人字典,然后映射记录,并从字典中添加联系人详细信息。

const { overArgs, identity, partialRight: pr, groupBy } = _

const fn = overArgs(
  (records, contactsByLeadId) => records.map(o => ({ ...o, contact_details: contactsByLeadId[o.lead_id] })),
  [identity, pr(groupBy, 'lead_id')]
)

const records = [{ "lead_id": 95173, "user_id": 526, "first_name": "Aaron", "last_name": "De La Rosa", }, { "lead_id": 95972, "user_id": 459, "first_name": "Abba", "last_name": "Lorenzo", }, { "lead_id": 95974, "user_id": 459, "first_name": "Abba", "last_name": "Lorenzo", } ]
const contacts = [{ "lead_id": 95173, "lead_contact_number_type_id": 1, "lead_contact_number_id": 25, "lead_contact_number": "+1 206-501-4581" }, { "lead_id": 95972, "lead_contact_number_type_id": 1, "lead_contact_number_id": 26, "lead_contact_number": "+1 206-501-4582" }, { "lead_id": 95974, "lead_contact_number_type_id": 1, "lead_contact_number_id": 27, "lead_contact_number": "+1 206-501-4583" }, { "lead_id": 95173, "lead_contact_number_type_id": 1, "lead_contact_number_id": 28, "lead_contact_number": "+1 206-501-4584" } ]

const result = fn(records, contacts)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>