I am trying to parse a string with a following format
"value1=name1:value2=test1,value1=name2:value2=null,value1=null:value2=test3"
I would want this to be parsed as
[{ value1: "name1", value2: "test1"}, { value1: "name2", value2: "NA"},{ value1: "NA", value2: "test3"}]
If in case any value post splititng is null , just make is "NA". Since I am using split the null value is being coerced to string. Hence setting "NA" using nullish coalescing(??) is not working and hence it still returns null and not NA. It would also be helpful , If any body can suggest a simpler way to achieve this
Code that I have tried.
let input = "value1=name1:value2=test1,value1=name2:value2=null,value1=null:value2=test3";
let items = input.split(",");
let pairs = items.map((item) => {
let splitItems = item.split(":");
let value1 = splitItems[0].split("=")?? "NA";;
let value2 = splitItems[1].split("=") ?? "NA";
return { value1, value2 };
});
-
You can filter out matches containing null values.Mr. Polywhirl– Mr. Polywhirl2020年09月08日 16:48:00 +00:00Commented Sep 8, 2020 at 16:48
4 Answers 4
You could use the ternary operator
const str =
"value1=name1:value2=test1,value1=name2:value2=test2,value1=null:value2=test3"
const res = str.split(",").map((el) =>
el.split(":").reduce((acc, pair) => {
const [key, value = "NA"] = pair.split("=")
return {
...acc,
[key]: value === "null" ? "NA" : value,
}
}, {})
)
console.log(res)
2 Comments
{ [buildKey(key)]: ... } and custom your expected key with the key taken from inputYou may try using this:
(string || '').split("=") || "NA";
1 Comment
What about using Object.fromentries since your split shows two values in the format [],[].
let input = "value1=name1:value2=test1,value1=name2:value2=test2,value1=null:value2=test3";
let items = input.replace(/null/g, 'NA').split(",");
let pairs = items.map((item) => {
let splitItems = item.split(":");
let value5 = splitItems[0].split("=");
let value6 = splitItems[1].split("=");
return Object.fromEntries([value5,value6])
});
console.log(pairs)
Comments
maybe that is not a simpler but easy and saver way to parse your string, and it is versatile enough to provide key value pairs as many where provided
const input = "value1=name1:value2=test1:value3=name3:value4=test4,value1=name2:value2=test2,value1=null:value2=test3";
const mappedKey = {
value3: 'someNewKey1',
value4: 'someNewKey2',
};
const parseData = (input) => {
// replace directly all null values before splitting
const stream = input.replace(/null/g, 'NA');
// now split by comma and equal sign
const parsedStream = stream.split(",").map((fragment) => {
// build a key value object, to be able to collect n value pairs
const obj = {};
// split groups by -> :
const values = fragment.split(':').map((split) => {
// split key value pairs by -> =
return split.split('=');
});
// collect the key value pais into obj
values.forEach((data) => {
const [key, value] = data;
obj[mappedKey[key] || key] = value;
});
// return the final result
return obj;
});
return parsedStream;
}
window.onload = () => {
document.querySelector('.input').innerHTML = input;
document.querySelector('.result').innerHTML = JSON.stringify(parseData(input), null, 2);
}
<h3>Input:</h3>
<pre class="input"></pre>
<hr />
<h3>Parsed result:</h3>
<pre class="result"></pre>