-1
  1. Problem is that i have two array data and MfaData.
  2. if data.mfaserviceid same as mfaData.id then assign mfa_name to this array of object.
const mfaData = [
  {
    id: 1,
    mfa_name: "PASSWORD",
    isActive: true,
  },
  {
    id: 2,
    mfa_name: "PIN",
  },
  {
    id: 3,
    mfa_name: "GRID",
  },
  {
    id: 4,
    mfa_name: "PASSCODE",
  },
  {
    id: 5,
    mfa_name: "QUESTION",
  },
];

const data = [
  {
    id: 18,
    channelname: "POS",
    mfaserviceid: "3",
  },
  {
    id: 19,

    channelname: "POS",
    mfaserviceid: "2",
  },
  {
    id: 20,
    channelname: "POS",
    mfaserviceid: "1",
  },
  {
    id: 21,
    channelname: "POS",
    mfaserviceid: "1",
  },
  {
    id: 22,
    channelname: "POS",
    mfaserviceid: "2",
  },
  {
    id: 23,
    channelname: "POS",
    mfaserviceid: "3",
  },
  {
    id: 24,
    channelname: "POS",
    mfaserviceid: "4",
  },
  {
    id: 25,
    channelname: "POS",
    mfaserviceid: "5",
  },
  {
    id: 26,
    channelname: "POS",
    mfaserviceid: "1",
  },
  {
    id: 27,
    channelname: "POS",
    mfaserviceid: "2",
  },
  {
    id: 28,

    channelname: "POS",
    mfaserviceid: "3",
  },
  {
    id: 29,

    channelname: "POS",
    mfaserviceid: "4",
  },
  {
    id: 30,

    channelname: "POS",
    mfaserviceid: "5",
  },
  {
    id: 31,
    channelname: "UPI",
    mfaserviceid: "1",
  },
  {
    id: 32,
    channelname: "UPI",
    mfaserviceid: "2",
  },
];

logic i implementd but not working

const mergedRes = data.map((item, i) => Object.assign({}, item, mfaData[i]));

the result i want in this way

const mergedRes = [
  {
    id: 1,
    channelname: "POS",
    mfa_name: "PASSWORD", // this is from mfaData
    mfaserviceid: "3",
  },
  {
    id: 1,
    channelname: "POS",
    mfa_name: "PASSWORD", // here i want to get mfa_name from mfaData
    mfaserviceid: "3",
  },
];

/// Problem is that i have two array data and MfaData. if data.mfaserviceid same as mfaData.id then assign mfa_name to this array of object. //// /// Problem is that i have two array data and MfaData. if data.mfaserviceid same as mfaData.id then assign mfa_name to this array of object. ///

  • also [how to merge two arrays of objects in javascript?](https://stackoverflow.com/questions/71048528/how-to-merge-two-arrays-of-objects-in-javascript) – pilchard Oct 06 '22 at 07:01
  • *"if data.mfaserviceid same as mfaData.id then assign mfa_name to this array of object."* Why does the result have `id: 1` and `mfaserviceid:"3"`? Not only do they not match as numbers, they don't match types as well. – zer00ne Oct 06 '22 at 07:10

2 Answers2

0
for(let i = 0 ; i < mfaData.length ; i++){
    
    for(let j = 0 ; j < data.length ;j++ ){
        if(+data[j].mfaserviceid === mfaData[i].id){
            data[j].mfa_name = mfaData[i].mfa_name
        }
    }
    
}
console.log(data)

try this

HiddenOne1254
  • 146
  • 1
  • 6
0

What your asking is if data.mfaserviceid === mfaData.id then merge the mfa_name with the object from data. This cannot be done in a single map I do not think.

const mergedArray = [];

for (let i = 0; i < mfaData.length; i++) {
  const mfaElement = mfaData[i];

  for (let j = 0; j < data.length; j++) {
    const dataElement = data[j];

    if (parseInt(dataElement.mfaserviceid) === mfaElement.id) {
      mergedArray.push({ mfa_name: mfaElement.mfa_name, ...dataElement });
    }
  }
}

This is the slow way, I do not know what your non-functional requirements are. This loops through both arrays. mfaserviceid is a string so you have to cast it to a number and compare it to mfaElement.id. If they are equal, then we can use ES 6 spread operator to merge mfa_name with the existing object from data.