# Split two strings into key value pairs

**URL:** <https://discourse.nodered.org/t/split-two-strings-into-key-value-pairs/61458>\
**Category:** General\
**Tags:** function-node\
**Created:** [18 April 2022 19:08 UTC](https://discourse.nodered.org/t/split-two-strings-into-key-value-pairs/61458 "2022-04-18T19:08:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tas22](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tas22](https://discourse.nodered.org/u/tas22)\
**Post date:** [18 April 2022 19:08 UTC](https://discourse.nodered.org/t/split-two-strings-into-key-value-pairs/61458/1 "2022-04-18T19:08:45Z")

</div>

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:

```auto
{
  "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.

```auto
// 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!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 April 2022 19:45 UTC](https://discourse.nodered.org/t/split-two-strings-into-key-value-pairs/61458/2 "2022-04-18T19:45:44Z")

</div>

You were close.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c168b56b4c0e411ed917339f90b8ddc45c5f9c7d.png)

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![tas22](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tas22](https://discourse.nodered.org/u/tas22)\
**Post date:** [18 April 2022 19:59 UTC](https://discourse.nodered.org/t/split-two-strings-into-key-value-pairs/61458/3 "2022-04-18T19:59:00Z")

</div>

Thank you so much for your help @Steve-Mcl

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 May 2022 19:59 UTC](https://discourse.nodered.org/t/split-two-strings-into-key-value-pairs/61458/4 "2022-05-02T19:59:48Z")

</div>

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