skip to Main Content

Input arrays :

var arr1 = [{EmpID:'1',Name:'PB',EmpCode:'10001'},{EmpID:'2',Name:'SS',EmpCode:'10002'}]; 
var arr2 = [{EmpID:'1',Address:'GGN'},{EmpID:'2',Address:'SP-GGN'}];

Expected Output :

[{EmpID:'1',Name:'PB',EmpCode:'10001',Address:'GGN'},{EmpID:'2',Name:'SS',EmpCode:'10002',Address:'SP-GGN'}]; 

I tried below logic

function fun(){
    var arr1 = [{EmpID:'1',Name:'PB',EmpCode:'10001'},{EmpID:'2',Name:'SS',EmpCode:'10002'}]; 
    var arr2 = [{EmpID:'1',Address:'GGN'},{EmpID:'2',Address:'SP-GGN'}];

    var arr3= [];
        for(var k=0; k < arr1.length; k++){    
        arr3.push(arr1[k]);
        }

    for(var k=0; k < arr2.length; k++){    
        arr3.push(arr2[k]);
        }
    console.log(arr3);

   
        var arr4 = [...arr1, ...arr2];
        console.log(arr4);
}

fun();

output result :

[{"EmpID": "2", "Name": "SS", "EmpCode": "10002"},{EmpID: '2', Name: 'SS', EmpCode: '10002'},{EmpID: '1', Address: 'GGN'},{EmpID: '2', Address: 'SP-GGN'}]

We can’t use array.filter(), .find(), nested loop etc.., only customize logic is allowed.

Expected Output :

[{EmpID:'1',Name:'PB',EmpCode:'10001',Address:'GGN'},{EmpID:'2',Name:'SS',EmpCode:'10002',Address:'SP-GGN'}]; 

5

Answers


  1. To achieve what you want, you can use a nested for loop to match the EmpID from both arrays. I think that should work:

    function mergeArrays() {
      var arr1 = [{EmpID:'1',Name:'PB',EmpCode:'10001'},{EmpID:'2',Name:'SS',EmpCode:'10002'}]; 
      var arr2 = [{EmpID:'1',Address:'GGN'},{EmpID:'2',Address:'SP-GGN'}];
      var resultArr = [];
    
      for (var i = 0; i < arr1.length; i++) {
        for (var j = 0; j < arr2.length; j++) {
          if (arr1[i].EmpID === arr2[j].EmpID) {
            var mergedObj = {...arr1[i], ...arr2[j]};
            resultArr.push(mergedObj);
          }
        }
      }
    
      console.log(resultArr);
    }
    
    Login or Signup to reply.
  2. here my code

    const arr1 = [
      { EmpID: "1", Name: "PB", EmpCode: "10001" },
      { EmpID: "2", Name: "SS", EmpCode: "10002" },
    ];
    const arr2 = [
      { EmpID: "1", Address: "GGN" },
      { EmpID: "2", Address: "SP-GGN" },
    ];
    
    const arr3 = arr1.map((arr, index) => {
      const value = Object.values(arr1[index])[0];
      const indexArr2 = arr2.find((ar2) => ar2.EmpID === value);
    
      return (arr = { ...arr, ...indexArr2 });
    });
    
    console.log("arr3 :>> ", arr3);
    Login or Signup to reply.
  3. I have updated your code, there are some minor mistakes in what you are doing, you need to put a second loop in the first loop, so that you can match the EmpID and then you in the object Address needs to be added according to the EmpID and then you need to push the object in the arr3, as you can see in the code below.

    function fun(){
        var arr1 = [{EmpID:'1',Name:'PB',EmpCode:'10001'},{EmpID:'2',Name:'SS',EmpCode:'10002'}]; 
        var arr2 = [{EmpID:'1',Address:'GGN'},{EmpID:'2',Address:'SP-GGN'}];
    
        var arr3= [];
        for(var k=0; k < arr1.length; k++){    
            let obj = {...arr1[k]}
            for(var j=0; j < arr2.length; j++){
                if (arr1[k].EmpID === arr2[j].EmpID) {
                  obj = {...obj, "Address": arr2[j].Address}
                }
            }
            arr3.push(obj);
        }
    
        console.log(arr3);
    }
    
    fun();
    Login or Signup to reply.
  4. const arr1 = [{
      EmpID: '1',
      Name: 'PB',
      EmpCode: '10001'
    }, {
      EmpID: '2',
      Name: 'SS',
      EmpCode: '10002'
    }];
    
    const arr2 = [{
      EmpID: '1',
      Address: 'GGN'
    }, {
      EmpID: '2',
      Address: 'SP-GGN'
    }];
    
    const arr3 = arr1.map((arr) => {
      const x = arr2.filter((i) => arr.EmpID === i.EmpID)
      return { ...arr,
        ...x[0]
      }
    })
    
    console.log(arr3)
    Login or Signup to reply.
  5. You could use two loops behind with an object for grouping instead nested loops.

    This approach takes an object for grouping objects by EmpID and an array for the result set.

    If you are allowed to use Object.values, you could omit result and return only the values of references.

    function merge(a1, a2) {
        function add(array) {
            for (const object of array) {
                if (!references[object.EmpID]) {
                    result.push(references[object.EmpID] = {});
                }
                for (const key in object) {
                    references[object.EmpID][key] = object[key];
                }
            }
        }
        const
            references = {},
            result = [];
    
        add(a1);
        add(a2);
        return result;
    }
    
    const
        array1 = [{ EmpID: '1', Name: 'PB', EmpCode: '10001' }, { EmpID: '2', Name: 'SS', EmpCode: '10002' }],
        array2 = [{ EmpID: '1', Address: 'GGN' }, { EmpID: '2', Address: 'SP-GGN' }]
        result = merge(array1, array2);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search