Home > Enterprise >  Insert all properties from an object within an array to another object in array using JS/TS
Insert all properties from an object within an array to another object in array using JS/TS

Time:07-01

I have been looking a simple way to copy/insert/move properties in an object within an array to another object. I came up with a basic logic which does the job perfectly but am not satisfied with this. There has to be a better way, any help here?

var first =  [
    {
        "AGREE_EFF_DATE__0": "02-Aug-2018",
        "AGREE_TERM_DATE__0": "30-Apr-2021",
        "AGREE_IND__0": "P1",
        "P_DBAR_IND__0": "N",
        "AGREE_EFF_DATE__1": "01-May-2021",
        "AGREE_TERM_DATE__1": null,
        "AGREE_IND__1": "NP",
        "P_DBAR_IND__1": "N",
        "PROVIDER_SPECIALITY__0": "PSYCHOLOGY, CLINICAL",
        "PROVIDER_SPECIALITY_CODE__0": "CK"
    }
];
var second = [
    {
        "STATUS": "ACTIVE",
        "MEDICARE_NUMBER" : 12345
    }
];

for(let i = 0; i < second.length; i  ) {
    
    var first_keys = Object.keys(first[i]);
    var first_values = Object.values(first[i]);
    
    for(let j = 0; j < first_keys.length; j  ) {
        second[i][first_keys[j]] = first_values[j];
    }
}


console.log(second);

//Output-
[
  {
    STATUS: 'ACTIVE',
    MEDICARE_NUMBER: 12345,
    AGREE_EFF_DATE__0: '02-Aug-2018',
    AGREE_TERM_DATE__0: '30-Apr-2021',
    AGREE_IND__0: 'P1',
    P_DBAR_IND__0: 'N',
    AGREE_EFF_DATE__1: '01-May-2021',
    AGREE_TERM_DATE__1: null,
    AGREE_IND__1: 'NP',
    P_DBAR_IND__1: 'N',
    PROVIDER_SPECIALITY__0: 'PSYCHOLOGY, CLINICAL',
    PROVIDER_SPECIALITY_CODE__0: 'CK'
  }
]

CodePudding user response:

You need to be careful with iterating, because you can have different count of elements in first and second arrays. So the possible solution will be like this:

const first = [
    {
        "AGREE_EFF_DATE__0": "02-Aug-2018",
        "AGREE_TERM_DATE__0": "30-Apr-2021",
        "AGREE_IND__0": "P1",
        "P_DBAR_IND__0": "N",
        "AGREE_EFF_DATE__1": "01-May-2021",
        "AGREE_TERM_DATE__1": null,
        "AGREE_IND__1": "NP",
        "P_DBAR_IND__1": "N",
        "PROVIDER_SPECIALITY__0": "PSYCHOLOGY, CLINICAL",
        "PROVIDER_SPECIALITY_CODE__0": "CK"
    }
];
const second = [
    {
        "STATUS": "ACTIVE",
        "MEDICARE_NUMBER": 12345
    }
];

console.log(mergeAll(first, second));

function mergeAll(firstArray, secondArray) {
    const result = [];
    const minLength = firstArray.length < secondArray.length ? firstArray.length : secondArray.length;

    for (let i = 0; i < minLength; i  ) {
        result.push({...firstArray[i], ...secondArray[i]});
    }

    return result;
}

CodePudding user response:

When possible, you should prefer iteration to manually indexed loops. This means arr.map() or arr.forEach() or arr.reduce(), to name a few.

Also, You can use an object spread to easily merge objects together.

Putting those together, you can reduce this logic to:

const result = first.map((firstObj, i) => ({ ...firstObj, ...second[i] }))

Here we map() over all members of first, which returns a new array where each member is the result of the function. This function takes the array member as the first argument, and the index of that member as the second argument. Then we can use that index to find the corresponding item in the second array.

Then you just spread both objects into a new object to assemble the final result.

var first =  [
    { a: 1, b: 2 },
    { a: 4, b: 5 },
];
var second = [
    { c: 3 },
    { c: 6 },
];

const result = first.map((firstObj, i) => ({ ...firstObj, ...second[i] }))

console.log(result)

Which is all perfectly valid typescript as well.


NOTE: there is one difference between my code any yours. Your code modifies the objects in second. My code returns new objects and does not change the contents of second at all.

This is usually the better choice, but it depends on how you use this value and how data is expected to flow around your program.

  • Related