Mike Victoria
Mike Victoria

Reputation: 254

How to insert an object of array or value and transfer into new data

I have two data called records and contacts, both have different lead_id's. If lead_id's are equally the same number this object is indicates that they will connect or insert and transform into new newRecords

I made an example data that will represent as example. I hope you could give an example.

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

expected output

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

Sample code

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

Upvotes: 0

Views: 48

Answers (4)

Ori Drori
Ori Drori

Reputation: 193012

I would generate a dictionary of concats by lead_id (O(n) - n number of contacts), and then map the records, and get the contacts by the lead_id from the map (O(m) - m number of records) to a total O(n + m).

Using a filter inside a map is O(m * n), since you need to filter the entire contacts array every time.

I've used _.overArgs() to generate a function the accepts records and contacts. The function creates a dictionary of contacts by lead_id with _.groupBy(), and then maps the records, and adds the contacts details from the dictionary.

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>

Upvotes: 0

Akrion
Akrion

Reputation: 18525

Another approach you can take is to group the contacts by the lead_id via Array.reduce. After that map through the records and compose your final object via accessing the contacts by the lead_id key.

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)

This method should be pretty performant since you do not on every iteration filter against the contacts array.

Upvotes: 1

Beginner
Beginner

Reputation: 9135

@MikeVictoria your attempt was correct, but small changes. You can reduce it by using map and filter so map through records will give you a new copy of same length array of records now then return all the properties while iteration and add a new property to each object so we add contact_details as a property and do a filter based on the lead_id contacts so it will filter all the objects and gives a new array which will be set to contact_details property

I hope this will solve your issue. Please let me know if anything I have missed

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)

Upvotes: 1

Nabin Paudyal
Nabin Paudyal

Reputation: 1639

You could apply a combination of map and filter methods here.

Apply a map and add contacts filtered by matching the lead_id as the new contact_details field for each record.

The following code should do the trick.

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

Upvotes: 2

Related Questions