Update the array with attributes from different array with same key for both - javascript

Vishnu

I have the following array which is assumed to be large data set.

let response1 = [
  { userID: '2222', dataOne: [ [Object], [Object] ] },
  {
    userID: '6666',
    dataOne: [ [Object] ],
    dataTwo: [ [Object], [Object] ]
  },
  {
    userID: '11111',
    dataOne: [ [Object], [Object] ],
    dataTwo: [ [Object] ]
  },
  { userID: '4586', dataTwo: [ [Object] ] }
];

I have another array which i got as a result of database query (which is also a large data set)

let dbResponse  = [{
  "attributes": {
    "dob": "19890147",
    "gender": "M",
    "mobilePhone": "1239000000",
    "name": "Ketan Hol",
  },
  "doctorID": "ds45ds",
  "userID": "11111"
},
{
  "attributes": {
    "dob": "19890386",
    "gender": "M",
    "mobilePhone": "1239000000",
    "name": "Sachin",
  },
  "doctorID": "erjjkrel",
  "userID": "6666"
},
{
  "attributes": {
    "dob": "19890219",
    "gender": "M",
    "mobilePhone": "1239000000",
    "name": "Vishwas",
  },
  "doctorID": "dfgfdg",
  "userID": "2222"
},
{
  "attributes": {
    "dob": "19890219",
    "gender": "M",
    "mobilePhone": "1239000000",
    "name": "Jis",
  },
  "doctorID": "dfgfdg",
  "userID": "98645"
},
{
  "attributes": {
    "dob": "19890219",
    "gender": "M",
    "mobilePhone": "1239000000",
    "name": "Brad",
  },
  "doctorID": "dfgfdg",
  "userID": "4586"
},
    {
          "attributes": {
            "dob": "19890219",
            "gender": "M",
            "mobilePhone": "1239000000",
            "name": "Brad",
          },
          "doctorID": "dfgfdg",
          "userID": "4586"
        }

];

I need to add the attributes such as dob, name from dbResponse to response1 array based on same userID. All the userID in response1 array should be populated with attributes like dob, name from dbResponse. I am confused on how to perform the below in large data set.

Expected output will be like this:

response1 = [
      { userID: '2222', dataOne: [ [Object], [Object] ], dob: '19890219', name: 'Vishwas' },
      {
        userID: '6666',
        dataOne: [ [Object] ],
        dataTwo: [ [Object], [Object] ],
        dob: '19890386',
        name: 'Sachin'
      },
      {
        userID: '11111',
        dataOne: [ [Object], [Object] ],
        dataTwo: [ [Object] ],
        dob: '19890147',
        name: 'Ketan Hol'
      },
      { userID: '4586', dataTwo: [ [Object] ], dob: '19890219', name: 'Brad' }
    ];

What will be the best way to achieve this using es6 functions for a large data sets? I am new to these es6 functions. Any help would be really appreciated.

mappie

Approach1

Iterate dbResponse for every userId in response1, extract the object and copy the object in response1.

Approach2 (Optimised operation)

As both are large arrays, you will have to iterate dbResponse a large number of times. To optimize the operation of finding the response1 corresponding userID object in the dbResponse array, you could maintain a mapping to reduce the searching complexity.

const result = dbResponse.reduce((acc, obj) => {
    const { userID } = obj
    acc[userID] = obj;
    return acc;
}, {});
const finalResult = response1.reduce((acc, curr) => {
    const { userID } = curr
    const dbObj = result[userID] || {}
    acc.push({
        ...curr,
        ...dbObj
    })
    return acc;
}, []);

The final result will be in finalResult

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Assigning attributes from an array

분류에서Dev

How to get all dictionaries with some same attributes from an array to a new array?

분류에서Dev

Remove array key from multidimensional array

분류에서Dev

How to access attributes of php object inside a array of php objects from javascript

분류에서Dev

PHP How to sum values of the array of the same key

분류에서Dev

Compare array values with same key in PHP

분류에서Dev

Verify that the values within an array are the same or different

분류에서Dev

How to get clicked element from multiple element with the same name different index index of array

분류에서Dev

Word frequency for array of key/values on javascript

분류에서Dev

Convert array to Hashmap (key value pair) javascript

분류에서Dev

fetch json object from json array with a key

분류에서Dev

Selector from array - Jquery / Javascript

분류에서Dev

how is Array object different from other objects

분류에서Dev

Summing from one point of an array to another point of the same array in Ruby

분류에서Dev

Removing elements from embedded array by matching with different array in mongoose

분류에서Dev

How to update a key's value in an associative array in php

분류에서Dev

Adding multiple returned MySQL rows to the same array key element in PHP

분류에서Dev

array_combine with key has only one from array 1 and multiple values from array 2

분류에서Dev

Ruby array sort by multiple attributes

분류에서Dev

How to display same property from different objects in Javascript

분류에서Dev

JavaScript: Add JSON objects from one array to another array conditionally

분류에서Dev

sum of specific key for multiple similar keys in array of object - JavaScript

분류에서Dev

Javascript Next, Prev, Random from an array

분류에서Dev

Make json from array of objects javascript

분류에서Dev

simple javascript captcha based on strings from array

분류에서Dev

Move values from JSON to javascript array

분류에서Dev

Creating a List Object from Array in Javascript

분류에서Dev

Remove the item from array in javascript titanium

분류에서Dev

Javascript: Display Changing Text From Array

Related 관련 기사

  1. 1

    Assigning attributes from an array

  2. 2

    How to get all dictionaries with some same attributes from an array to a new array?

  3. 3

    Remove array key from multidimensional array

  4. 4

    How to access attributes of php object inside a array of php objects from javascript

  5. 5

    PHP How to sum values of the array of the same key

  6. 6

    Compare array values with same key in PHP

  7. 7

    Verify that the values within an array are the same or different

  8. 8

    How to get clicked element from multiple element with the same name different index index of array

  9. 9

    Word frequency for array of key/values on javascript

  10. 10

    Convert array to Hashmap (key value pair) javascript

  11. 11

    fetch json object from json array with a key

  12. 12

    Selector from array - Jquery / Javascript

  13. 13

    how is Array object different from other objects

  14. 14

    Summing from one point of an array to another point of the same array in Ruby

  15. 15

    Removing elements from embedded array by matching with different array in mongoose

  16. 16

    How to update a key's value in an associative array in php

  17. 17

    Adding multiple returned MySQL rows to the same array key element in PHP

  18. 18

    array_combine with key has only one from array 1 and multiple values from array 2

  19. 19

    Ruby array sort by multiple attributes

  20. 20

    How to display same property from different objects in Javascript

  21. 21

    JavaScript: Add JSON objects from one array to another array conditionally

  22. 22

    sum of specific key for multiple similar keys in array of object - JavaScript

  23. 23

    Javascript Next, Prev, Random from an array

  24. 24

    Make json from array of objects javascript

  25. 25

    simple javascript captcha based on strings from array

  26. 26

    Move values from JSON to javascript array

  27. 27

    Creating a List Object from Array in Javascript

  28. 28

    Remove the item from array in javascript titanium

  29. 29

    Javascript: Display Changing Text From Array

뜨겁다태그

보관