Split two strings into key value pairs

Hi guys, I am new to the forum! Excited to be apart of this community.
I was trying to research and figure out how to solve this problem. I have two key values with strings that I am merging into individual key value pairs.

I have tried via custom js function, here's a sample object I am trying to split out the labels and values into one:

{
  "sID": "0974607",
  "sName": "ltest",
  "aID": "43",
  "nID": "546",
  "dGUID": "34350",
  "state": "0",
  "mDate": "2022-04-16 21:01:25",
  "rData": "45.59%2c21.25%2c9%2c50.8%2c20.6%2c14.2",
  "dType": "Percentage|TemperatureData|TemperatureData|MoistureWeight|TemperatureData|TemperatureData",
  "dValue": "45.59|21.25|9|50.8|20.6|14.2",
  "pValues": "45.59|70.25|48.2|50.8|69.08|57.56",
  "pLabels": "Humidity|Fahrenheit|DewPoint|GrainsPerPound|HeatIndex_Fahrenheit|WetBulb_Fahrenheit",
  "Level": "100",
  "Strength": "89",
  "Change": "True",
  "volt": "2.98"
}

Here's my js function. You can try this in jsfiddle. I have tried this a few different ways but can't seem to get it right.

// var pL = "Humidity|Fahrenheit|DewPoint|GrainsPerPound|HeatIndex_Fahrenheit|WetBulb_Fahrenheit";
// var pV = "45.59|70.25|48.2|50.8|69.08|57.56";
var newMsg = () => {
	var pL = msg.payload.pLabels;
	var pV = msg.payload.pValues;
	var split1 = pL.split("|");
	var split2 = pV.split("|");
	var obj = {};
	for (i = 0; i <= split1.length; i++) {
  obj[split1[i]] = split2[i];
	}
	return obj;
}

var obj = newMsg();
// console.log(obj);

Any help would be appreciated!

You were close.

const makePayload = () => {
    const labels = msg.payload.pLabels.split("|");
    const values = msg.payload.pValues.split("|").map(e=>parseFloat(e));
    const obj = {};
    for (let i = 0; i < labels.length; i++) {
        obj[labels[i]] = values[i];
    }
    return obj;
}
msg.payload = makePayload();
return msg;

Thank you so much for your help @Steve-Mcl

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.