Home > Enterprise >  How to get this desired output by joining two array of objects
How to get this desired output by joining two array of objects

Time:07-20

I have two array of objects that has common key pin and an empty customer array. I'm trying to combine them as shown in expected output but couldn't figure out how to proceed. Can you please give me some pointers on how to achieve this?

I tried slicing the array2 data and then manually pushing data to array1 customer key but I'm getting duplicates. Please help me.

Array1 data

[
  { "id": 1, "email": "[email protected]", "pin": 600090, "customer": [] },
  { "id": 2, "email": "[email protected]", "pin": 600090, "customer": [] },
  { "id": 3, "email": "[email protected]", "pin": 600090, "customer": [] },
  { "id": 4, "email": "[email protected]", "pin": 600090, "customer": []},
]

Array2 data

[
  {
    "id": 1,
    "first_name": "Amble",
    "last_name": "Huntly",
    "email": "[email protected]",
    "gender": "Male",
    "addr": "30 Badeau Circle",
    "pin": 600090
  },
  {
    "id": 2,
    "first_name": "Karlene",
    "last_name": "Forrest",
    "email": "[email protected]",
    "gender": "Female",
    "addr": "9417 Sutherland Avenue",
    "pin": 600090
  },
  {
    "id": 3,
    "first_name": "Mel",
    "last_name": "Kayley",
    "email": "[email protected]",
    "gender": "Female",
    "addr": "7 Sage Crossing",
    "pin": 600090
  },
  {
    "id": 4,
    "first_name": "Nissa",
    "last_name": "Abelov",
    "email": "[email protected]",
    "gender": "Female",
    "addr": "2906 Fairfield Circle",
    "pin": 600090
  },
  {
    "id": 5,
    "first_name": "Benn",
    "last_name": "Calbrathe",
    "email": "[email protected]",
    "gender": "Bigender",
    "addr": "2914 Steensland Avenue",
    "pin": 600090
  },
  {
    "id": 6,
    "first_name": "Hobard",
    "last_name": "Toffanini",
    "email": "[email protected]",
    "gender": "Genderfluid",
    "addr": "33695 Luster Street",
    "pin": 600090
  },
  {
    "id": 7,
    "first_name": "Jabez",
    "last_name": "Arlidge",
    "email": "[email protected]",
    "gender": "Male",
    "addr": "1827 Harper Parkway",
    "pin": 600090
  },
  {
    "id": 8,
    "first_name": "Mitchel",
    "last_name": "Jessard",
    "email": "[email protected]",
    "gender": "Male",
    "addr": "52 Fieldstone Pass",
    "pin": 600090
  },
  {
    "id": 9,
    "first_name": "Robbyn",
    "last_name": "Kenningley",
    "email": "[email protected]",
    "gender": "Female",
    "addr": "44447 Golf Course Terrace",
    "pin": 600090
  },
  {
    "id": 10,
    "first_name": "Jaynell",
    "last_name": "todor",
    "email": "[email protected]",
    "gender": "Female",
    "addr": "3 Shoshone Junction",
    "pin": 600090
  },
]

I want the new array to have a length of Array1 i.e., 4. I just want to fill the customer array in each object of Array1 with Array2 objects equally except the last one.

So each customer gets Math.round(Array2.length/Array1.length) which is 2. I want each customer key in each object of array1 to be filled with 2 objects of array 2. Like shown below. Last dude gets assigned the remaining ojects as there are no more users in array1.

Expected output

[
  {
    "id": 1,
    "email": "[email protected]",
    "pin": 600090,
    "customer": [
      {
        "id": 1,
        "first_name": "Amble",
        "last_name": "Huntly",
        "email": "[email protected]",
        "gender": "Male",
        "addr": "30 Badeau Circle",
        "pin": 600090
      },
      {
        "id": 2,
        "first_name": "Karlene",
        "last_name": "Forrest",
        "email": "[email protected]",
        "gender": "Female",
        "addr": "9417 Sutherland Avenue",
        "pin": 600090
      }
    ]
  },
  {
    "id": 2,
    "email": "[email protected]",
    "pin": 600090,
    "customer": [
      {
        "id": 3,
        "first_name": "Mel",
        "last_name": "Kayley",
        "email": "[email protected]",
        "gender": "Female",
        "addr": "7 Sage Crossing",
        "pin": 600090
      },
      {
        "id": 4,
        "first_name": "Nissa",
        "last_name": "Abelov",
        "email": "[email protected]",
        "gender": "Female",
        "addr": "2906 Fairfield Circle",
        "pin": 600090
      }
    ]
  },
  {
    "id": 3,
    "email": "[email protected]",
    "pin": 600090,
    "customer": [
      {
        "id": 5,
        "first_name": "Benn",
        "last_name": "Calbrathe",
        "email": "[email protected]",
        "gender": "Bigender",
        "addr": "2914 Steensland Avenue",
        "pin": 600090
      },
      {
        "id": 6,
        "first_name": "Hobard",
        "last_name": "Toffanini",
        "email": "[email protected]",
        "gender": "Genderfluid",
        "addr": "33695 Luster Street",
        "pin": 600090
      }
    ]
  },
  {
    "id": 4,
    "email": "[email protected]",
    "pin": 600090,
    "customer": [
      {
        "id": 7,
        "first_name": "Jabez",
        "last_name": "Arlidge",
        "email": "[email protected]",
        "gender": "Male",
        "addr": "1827 Harper Parkway",
        "pin": 600090
      },
      {
        "id": 8,
        "first_name": "Mitchel",
        "last_name": "Jessard",
        "email": "[email protected]",
        "gender": "Male",
        "addr": "52 Fieldstone Pass",
        "pin": 600090
      },
      {
        "id": 9,
        "first_name": "Robbyn",
        "last_name": "Kenningley",
        "email": "[email protected]",
        "gender": "Female",
        "addr": "44447 Golf Course Terrace",
        "pin": 600090
      },
      {
        "id": 10,
        "first_name": "Jaynell",
        "last_name": "todor",
        "email": "[email protected]",
        "gender": "Female",
        "addr": "3 Shoshone Junction",
        "pin": 600090
      }
    ]
  }
]

CodePudding user response:

You can use for loop and splice() for doing that

const arr1 = [{
    id: 1,
    email: "[email protected]",
    pin: 600090,
    customer: []
  },
  {
    id: 2,
    email: "[email protected]",
    pin: 600090,
    customer: []
  },
  {
    id: 3,
    email: "[email protected]",
    pin: 600090,
    customer: []
  },
  {
    id: 4,
    email: "[email protected]",
    pin: 600090,
    customer: []
  },
];
const arr2 = [{
    id: 1,
    first_name: "Amble",
    last_name: "Huntly",
    email: "[email protected]",
    gender: "Male",
    addr: "30 Badeau Circle",
    pin: 600090,
  },
  {
    id: 2,
    first_name: "Karlene",
    last_name: "Forrest",
    email: "[email protected]",
    gender: "Female",
    addr: "9417 Sutherland Avenue",
    pin: 600090,
  },
  {
    id: 3,
    first_name: "Mel",
    last_name: "Kayley",
    email: "[email protected]",
    gender: "Female",
    addr: "7 Sage Crossing",
    pin: 600090,
  },
  {
    id: 4,
    first_name: "Nissa",
    last_name: "Abelov",
    email: "[email protected]",
    gender: "Female",
    addr: "2906 Fairfield Circle",
    pin: 600090,
  },
  {
    id: 5,
    first_name: "Benn",
    last_name: "Calbrathe",
    email: "[email protected]",
    gender: "Bigender",
    addr: "2914 Steensland Avenue",
    pin: 600090,
  },
  {
    id: 6,
    first_name: "Hobard",
    last_name: "Toffanini",
    email: "[email protected]",
    gender: "Genderfluid",
    addr: "33695 Luster Street",
    pin: 600090,
  },
  {
    id: 7,
    first_name: "Jabez",
    last_name: "Arlidge",
    email: "[email protected]",
    gender: "Male",
    addr: "1827 Harper Parkway",
    pin: 600090,
  },
  {
    id: 8,
    first_name: "Mitchel",
    last_name: "Jessard",
    email: "[email protected]",
    gender: "Male",
    addr: "52 Fieldstone Pass",
    pin: 600090,
  },
  {
    id: 9,
    first_name: "Robbyn",
    last_name: "Kenningley",
    email: "[email protected]",
    gender: "Female",
    addr: "44447 Golf Course Terrace",
    pin: 600090,
  },
  {
    id: 10,
    first_name: "Jaynell",
    last_name: "todor",
    email: "[email protected]",
    gender: "Female",
    addr: "3 Shoshone Junction",
    pin: 600090,
  },
];

const count = Math.floor(arr2.length / arr1.length);

for (let i = 0; i < arr1.length; i  ) {
  const remaining = arr2.length;
  if (i === arr1.length - 1) {
    arr1[i].customer.push(...arr2);
    break;
  }
  arr1[i].customer.push(...arr2.splice(0, count));
}
console.log(arr1);

CodePudding user response:

Let's define arr1 as our first array and arr2 as the second array :

let fillCustomers = function(arr1, arr2) {
    const customerPerArray = Math.floor(arr2.length / arr1.length);

    for(let el of arr1) {
        for(let i = 0; i < customerPerArray;   i) {
            el.customer.push(arr2.shift());
        }
    }

    for(let i in arr2) {
        arr1[i].customer.push(arr2.shift());
    }

    return arr1;
}

let result = fillCustomers([...arr1], [...arr2]);
console.log(result);
  • Related