Home > Back-end >  Javascript: Merge 2 arrays of objects into a new one, based on object properties
Javascript: Merge 2 arrays of objects into a new one, based on object properties

Time:03-31

I have 2 arrays of JSON objects in javascript from which I would like to create a new array where I map on the property "Word_No" They look like this:

wordInfo (length 4000):

    [
    {
        "Word_No": "0",
        "Alarm_Bit": "0",
        "Alarm_No": "1",
        "Alarm_Description": "Alarm text 1"
    },
    {
        "Word_No": "0",
        "Alarm_Bit": "1",
        "Alarm_No": "2",
        "Alarm_Description": "Alarm text 2"
    },
    {
        "Word_No": "0",
        "Alarm_Bit": "2",
        "Alarm_No": "3",
        "Alarm_Description": "Alarm text 3"
    }
]

and wordTags (length 250):

    [
    {
        "Word_No": "0",
        "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm0_15"
    },
    {
        "Word_No": "1",
        "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm16_31"
    },
    {
        "Word_No": "2",
        "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm32_47"
    }
]

What I need to become is a new array: Alarmlist (length 4000):

    [
    {
        "OPC Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm0_15",
        "Alarm_Bit": "0",
        "Alarm_No": "1",
        "Alarm_Description": "Alarm text 1"
    },
    {
        "OPC Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm0_15",
        "Alarm_Bit": "1",
        "Alarm_No": "2",
        "Alarm_Description": "Alarm text 2"
    },
    {
        "OPC Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm0_15",
        "Alarm_Bit": "2",
        "Alarm_No": "3",
        "Alarm_Description": "Alarm text 3"
    }
]

When I try to build this function using 2 for loops and mapping on the the Word_No property, it crashes.

    for (i = 0; i < wordInfo.length;   i){  //Outer loop  
    for (j = 0; j < wordTags.length;   j){  //Inner loop
        msg.Info_Word_No = wordInfo[i].Word_No //Woordnr
        msg.Tag_Word_No = wordTags[j].Word_No //Woordnr
        node.send(msg);
    } 
}

When I limit i and j to for example 10, the function executes and shows the word numbers in the debug window.

My idea was to map everything like this:

if(wordInfo[i].Word_No == wordTags[i].Word_No){
            var alarmTagInfo;
            alarmTagInfo.Alarm_No=wordInfo[i].Alarm_No;
            alarmTagInfo.OPC_Tag = wordTags[i].OPC_Tag;
            alarmTagInfo.Alarm_Bit = wordInfo[i].Alarm_Bit;
            msg.payload = alarmTagInfo;
            alarmlist.push(alarmTagInfo);
        }

But since the arrays are too big, the Node Red application just crashes. I'm not sure what would be the efficient way to handle this?

CodePudding user response:

I'd suggest first creating a lookup object for OPC_Tag using Array.reduce() on wordTags - this will improve performance and avoid doing a .find() for each iteration of the loop on wordInfo.

We'd then use Array.map() on wordInfo to create the final result:

let wordInfo = [ { "Word_No": "0", "Alarm_Bit": "0", "Alarm_No": "1", "Alarm_Description": "Alarm text 1" }, { "Word_No": "0", "Alarm_Bit": "1", "Alarm_No": "2", "Alarm_Description": "Alarm text 2" }, { "Word_No": "0", "Alarm_Bit": "2", "Alarm_No": "3", "Alarm_Description": "Alarm text 3" } ]
let wordTags = [ { "Word_No": "0", "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm0_15" }, { "Word_No": "1", "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm16_31" }, { "Word_No": "2", "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm32_47" } ] 

// Create a lookup, mapping Word_No to OPC_Tag
let wordTagsLookup = wordTags.reduce((acc, { Word_No, OPC_Tag }) => { 
    acc[Word_No] = OPC_Tag;
    return acc;
}, {})

let result = wordInfo.map(({ Word_No, Alarm_Bit, Alarm_No, Alarm_Description}) => { 
    return { Alarm_Bit, Alarm_No, Alarm_Description, OPC_Tag: wordTagsLookup[Word_No] };
})

console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; }

CodePudding user response:

I would suggest to create a Map for the second array, keyed by Word_No. Then map that array to objects where the translation is made with that Map. With object destructuring this can become quite elegant:

// Sample input data
const wordInfo = [{"Word_No": "0","Alarm_Bit": "0","Alarm_No": "1","Alarm_Description": "Alarm text 1"},{"Word_No": "0","Alarm_Bit": "1","Alarm_No": "2","Alarm_Description": "Alarm text 2"},{"Word_No": "0","Alarm_Bit": "2","Alarm_No": "3","Alarm_Description": "Alarm text 3"}];
const wordTags = [{"Word_No": "0","OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm0_15"},{"Word_No": "1","OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm16_31"},{"Word_No": "2","OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm32_47"}];

// Create map keyed by Word_No:
const map = new Map(wordTags.map(({Word_No, OPC_Tag}) => ([Word_No, OPC_Tag])));
// Translate the source data to the target data
const alarmList = wordInfo .map(({Word_No, ...rest}) => 
    ({OPC_Tag: map.get(Word_No), ...rest})
); 
console.log(alarmList);

CodePudding user response:

for me you can make like this, just using .map() and .filter() method :

const wordInfo = [
  {
      "Word_No": "0",
      "Alarm_Bit": "0",
      "Alarm_No": "1",
      "Alarm_Description": "Alarm text 1"
  },
  {
      "Word_No": "0",
      "Alarm_Bit": "1",
      "Alarm_No": "2",
      "Alarm_Description": "Alarm text 2"
  },
  {
      "Word_No": "0",
      "Alarm_Bit": "2",
      "Alarm_No": "3",
      "Alarm_Description": "Alarm text 3"
  },
];

const wordTags = [
    {
        "Word_No": "0",
        "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm0_15"
    },
    {
        "Word_No": "1",
        "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm16_31"
    },
    {
        "Word_No": "2",
        "OPC_Tag": "HH.Application.TmpHmi_Var.TmpHmiC7.Alarm32_47"
    }
];

const data = wordInfo.map((wordInfoItem) => {
  const wordTag = wordTags
    .filter((wordTagItem) => (wordInfoItem["Word_No"] === wordTagItem["Word_No"]))[0];
  
  const newArray = {
    ...wordTag,
    ...wordInfoItem,
  };

  delete newArray['Word_No'];
  return newArray;
});

console.log(data);
  • Related