Convert string into key-value pairs in an array - javascript

I have a string:
var rrule = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
I want to convert this string to key-> value pairs in an array.
[
dtstart: 20190514T111500Z,
freq: daily,
interval: 1
]
I know I can take the string and split it based on the semicolon:
var array = rrule.split(";");
... but this leaves me with an array like this:
[
"DTSTART=20190514T111500Z",
"FREQ=DAILY",
"INTERVAL=1"
]
I guess I need another step to map out the keys/values, but I get lost at this point.
Ideally, for the string I want to be able to easily access what dtstarts equals, what interval equals, what other variables equal and so on.

let str = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
let obj = {};
for (let entry of str.split(";")) {
let pair = entry.split("=");
obj[pair[0]] = pair[1];
}
console.log(obj);

You already know how to split on the ; to get an array, from there you can just aggregate (using reduce) to get an object:
var rrule = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
var result = rrule.split(";").reduce( (obj,item) => {
let [key,value] = item.split("=");
obj[key] = value;
return obj;
},{});
console.log(result["DTSTART"])
console.log(result["FREQ"])
console.log(result["INTERVAL"])

You were correct to start with split first, this would then return you an array of strings.
To easily convert them, just use map, to return the split the single strings once more, and then return an object based on the property name you would like to give it and it's value
function createKeyValuePairFromString( str ) {
return str.split(';').map( item => {
const splitted = item.split('=');
return { [splitted[0]]: splitted[1] };
});
}
console.log( createKeyValuePairFromString("DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1") );

Use array created and split it again with =
function convertToObject(cookieString) {
const cookieObj = {};
if (!cookieString && typeof cookieString !== 'string') return cookieObj;
const arr = cookieString.split(';');
arr.forEach(record => {
if (record.includes('=')) {
const [key, value] = record.split('=');
cookieObj[key.trim()] = value;
}
});
return cookieObj;
}

You can use it like the code below:
var rrule = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
let finalObj = {};
rrule.split(';').forEach(i => finalObj[i.split('=')[0]] = i.split('=')[1]);
console.log('finalObj',finalObj);
Here I'm first splitting with ';' so consider the first item to be DTSTART=20190514T111500Z Then on splitting with = I get finalObject['DTSTART'] = 20190514T111500Z

Using forEach()
let str = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
let obj = {};
let strArr = str.split(';')
strArr.forEach((str) => {
let [key, value] = str.split('=')
obj[key] = value;
});
console.log(obj);

Here's a fairly simple version, returning an object, not an array:
const toObj = str => str
.split (';')
.map ( s => s .split ('=') )
.reduce ( (a, [k, v]) => ({...a, [k]: v}), {} )
let str = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
console.log (
toObj(str)
)
One of the reasons I like the library is that we can write this sort of logic more simply. In Ramda (disclaimer: I'm one of the authors), it might look like this:
const toObj = pipe ( split (';'), map (split ('=') ), fromPairs)
let str = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
console.log (
toObj(str)
)
<script src="https://bundle.run/ramda#0.26.1"></script><script>
const {pipe, split, map, fromPairs} = ramda; </script>

var str = "DTSTART=20190514T111500Z;FREQ=DAILY;INTERVAL=1";
// string splitting rule
const rule = (string, delimiter) => string.split(delimiter);
const result = rule(str, ';').reduce((acc, s) => {
const [key, value] = rule(s, '=');
acc[key] = value;
return acc;
}, {});
console.log(result);

Related

Get Key Value pair after String Split

I have a string in the form
Key=asdf, num=90, Key=ert, num=20, Key=yged, num=20, Key=kned, num=35
I have to filter only Key num pairs which has value 20 and store them into a Key Value pair such that Key=ert, num=20 will be first record and Key=yged, num=20 will be second record so on. How can I use Map in JavaScript so that always first value will go as key and second will go as value and form pairs in this case. I have used the following :
var dataString = JSON.parse(data).data;
var arrayVal = new Array();
arrayVal = dataString.split(', ');
for(a in arrayVal){
console.log(arrayVal[a]);
}
Array.map probably isn't the best tool for the job here. Array.reduce would be a better approach. Since you know what your delimiters are (namespaces and equal signs), you can logically split things up so that you know that every other iteration will give you a key/value. You can then create a mechanism to track what the last key is so you can map the value to it.
const str = 'Key=asdf, num=90, Key=ert, num=20, Key=yged, num=20, Key=kned, num=35';
const arr = str.split(', ').reduce( (acc, curr) => {
const entry = curr.split('=');
const key = entry[0];
const val = entry[1];
if (key === 'Key') {
acc['last_key'] = val
acc[val] = null;
} else if (key === 'num') {
acc[acc['last_key']] = val;
}
return acc;
}, {});
delete arr.last_key;
console.log(arr);
Here you go. It's kinda ugly.
let result = dataString.split(', ')
.map((value, index, array) =>
value === 'num=20' && array[index - 1])
.filter(x => x);
console.log(result);
Here's my say
const dataString = JSON.parse(data).data;
const arrayVal = dataString.split(', ');
const obj = {}; // initialize the object
for (let i = 0; i < arrayVal.length; i += 2) { // Iterating over two elements for the key and value
const key = arrayVal[i].split('=')[1];
const value = arrayVal[i + 1].split('=')[1];
obj[key] = value;
}
console.log(obj);

Javascript searching task

Task: You have array with key: value pairs like:
arr = ["x:1", "y:2", "a:5", "x:5", "z:0"]
You must sum all the same keys so it will be:
["x:6","y:2","a:5","z:0"]
on output.
I finish this task but its complicate. I have solved the task but I am looking for a simpler solution
var a = ["x:1", "y:2", "a:5", "x:5", "z:0"]
function saberi(niz) {
let povratni = [];
for (clan of niz) {
let razdvojen = clan.split(":");
let key = razdvojen[0]
let value = razdvojen[1]
let provjera = false
for (let i = 0; i < povratni.length; i++) {
let razdvojen2 = povratni[i].split(":")
let key2 = razdvojen2[0]
let value2 = razdvojen2[1]
if (key2 == key) {
console.log("uso")
let novavrijednost = parseFloat(value) + parseFloat(value2)
povratni[i] = key2 + ":" + novavrijednost
provjera = true
break
}
}
if (!provjera) {
povratni.push(clan)
}
}
console.log(povratni)
return povratni
}
saberi(a)
You can use objects to make this easier. Each character to the left of the colon : will be a key in the object, and the number to the right of the colon will be the value, which you can accumulate to the value from the object at the given key.
To start, you can loop through your arr using a for...of loop, and for each string in your array, use .split(":") to break the string into its key and value components. You can then grab the current value stored at the key from the object (grouped) by using grouped[key]. If the value doesn't exist, you can default it to 0 using || 0. Once you have the current value stored in the object, you can add the number version of the value to the accumulated value, and store that new value in the object at the key key.
Once you have your object, you can grab the entries to get an array of the form [[key, value], [key2, value2], ...], which you can use .map() on to convert each [key, value] to a string of the form key:value:
const arr = ["x:1", "y:2", "a:5", "x:5", "z:0"];
const grouped = {};
for(const str of arr) {
const [key, num] = str.split(":");
grouped[key] = (grouped[key] || 0) + Number(num);
}
const res = Object.entries(grouped).map(entry => entry.join(":"));
console.log(res);
The same concept above can be accomplished using .reduce() with a Map instead of an object, as well as Array.from to map the entries of the Map to an array:
const arr = ["x:1", "y:2", "a:5", "x:5", "z:0"];
const res = Array.from(arr.reduce((map, str) => {
const [key, val] = str.split(":");
return map.set(key, (map.get(key) || 0) + +val);
}, new Map), entry => entry.join(':'));
console.log(res);
You could use Array.prototype.reduce() method. Traverse the array and split each item by colon(:). Then group it by key to count sum. At last, transform the object into a key-value pair array using Object.entries() method and then map and join it by colon(:).
const arr = ['x:1', 'y:2', 'a:5', 'x:5', 'z:0'];
const ret = Object.entries(
arr.reduce((prev, c) => {
const p = prev;
const [k, v] = c.split(':');
p[k] = p[k] ?? 0;
p[k] += +v;
return p;
}, {})
).map((x) => x.join(':'));
console.log(ret);

Convert string with ampersands into a key value map in Javascript (node js)

I have a string that has parameters separated with ampersands , for example :
orderid=55e3a83e&DealId=545435&Amount=590 ....
How can I convert that into a Key/Value Map ?
My current code is as follows :
const text = "orderid=55e3a83e&DealId=545435&Amount=590 ...."
let _key = "";
let _value = "";
var myregexp = /([^&=]+)=(.*?)(?=&[^&=]+=|$)/g;
var match = myregexp.exec(text);
while (match != null && key !== "url") {
_key = match[1];
_value = match[2];
dict.push({
key: _key,
value: _value
});
match = myregexp.exec(subject);
}
But it's too long , any idea for something better or shorter ?
You can split first by & and then split each element by =, and build a key/value pair
let mapper = (str) => str.split('&').filter(Boolean).map(v => {
let [key, value] = v.split('=')
return { key, value }
})
console.log(mapper('orderid=55e3a83e&DealId=545435&Amount=590 '))
In case desired output in the form of a single object
let mapper = (str) => {
let splitted = str.split('&').map(v => v && v.split('=')).filter(Boolean)
return Object.fromEntries(splitted)
}
console.log(mapper('orderid=55e3a83e&DealId=545435&Amount=590 '))
I've always used some code very similar to your current solution:
function getUrlParams(input) {
var params = {};
input.replace(/[?&]+([^=&]+)=([^&]*)/g, (m, key, value) => {
params[key] = value;
});
return params;
}
Of course, this doesn't ignore the URL parameter, and it returns a plain JS object... but you get the idea.
The string looks like a URL query, you can use URLSearchParams to parse it, here is an example:
const usp = new URLSearchParams('orderid=55e3a83e&DealId=545435&Amount=590');
const dictionary = [];
for (const [key, value] of usp.entries()) {
dictionary.push({ key, value})
}
console.log(dictionary);

How to convert the string to object in JavaScript?

I need to convert this string into object
var str = "res=[xyz=name,abc=address]";
I need below output :
var obj = {xyz: "name",abc:"address"}
I tried to convert this by using json.parse and split but I can't get the result .can anyone help to do this ?
This code works with the string given. It is also very readable for noobs
var str = "res=[xyz=name,abc=address]";
str = str.split("res=")[1]
.replace("[",'{"')
.replace("]",'"}')
.replace(/=/g,'":"')
.replace(/,/g,'","');
res = JSON.parse(str);
console.log(str,"\n",res);
One more Implementation:
//str format "res=[xyz=name,abc=address]"
function stringToObject(str){
const extractedStr = str.substring(str.indexOf("[")+1,str.indexOf("]")-1);
return extractedStr.split(",").reduce((acc,keyVal)=>{
acc[keyVal.split("=")[0]] = keyVal.split("=")[1];
return acc;
},{});
}
console.log(stringToObject("res=[xyz=name,abc=address]"));
Here's a way to do it:
// format your input like this, comma separated, and key=val
const str = "xyz=name,abc=address"
const obj = str.split(',').reduce((acc, keyVal) => {
const [key, val] = keyVal.split('=')
acc[key] = val
return acc
}, {})
console.log(obj)
// { xyz: "name", abc: "address" }
Another implementation
let str = "res=[xyz=name,abc=address]";
let convertStrToObject = (str) => {
return str
.substring(5, str.length - 1)
.split(",")
.reduce((acc, item) => {
acc[item.split("=")[0]] = item.split("=")[1];
return acc;
}, {});
};
console.log(convertStrToObject(str));

Splitting string to object in JS

I get a response from a server like this:
3S1,https://lekcjaplus.vulcan.net.pl
TA1,https://uonetplus-komunikacja.umt.tarnow.pl
OP1,https://uonetplus-komunikacja.eszkola.opolskie.pl
RZ1,https://uonetplus-komunikacja.resman.pl
GD1,https://uonetplus-komunikacja.edu.gdansk.pl
P03,https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl
P01,http://efeb-komunikacja.pro-hudson.win.vulcan.pl
P02,http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl
P90,http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl
I want to convert it to an object like this:
"3S1": "https://lekcjaplus.vulcan.net.pl",
"TA1": "https://uonetplus-komunikacja.umt.tarnow.pl",
"OP1": "https://uonetplus-komunikacja.eszkola.opolskie.pl",
"RZ1": "https://uonetplus-komunikacja.resman.pl",
"GD1": "https://uonetplus-komunikacja.edu.gdansk.pl",
"P03": "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl",
"P01": "http://efeb-komunikacja.pro-hudson.win.vulcan.pl",
"P02": "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl",
"P90": "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl"
What's the simplest way to achieve this?
You can split by new line and use reduce
let str = `3S1,https://lekcjaplus.vulcan.net.pl
TA1,https://uonetplus-komunikacja.umt.tarnow.pl
OP1,https://uonetplus-komunikacja.eszkola.opolskie.pl
RZ1,https://uonetplus-komunikacja.resman.pl
GD1,https://uonetplus-komunikacja.edu.gdansk.pl
P03,https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl
P01,http://efeb-komunikacja.pro-hudson.win.vulcan.pl
P02,http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl
P90,http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl`;
let result = str.split(/\n/).reduce((c, v) => {
if( v.trim() !== '' ) {
let [k, o] = v.trim().split(',');
c[k] = o;
}
return c;
}, {});
console.log(result);
In case you have multiple , on each line, you can deconstruct the array and join(',')
let result = str.split(/\n/).reduce((c,v)=>{
if( v.trim() ) {
let [k,...o] = v.trim().split(',');
c[k] = o.join(',');
}
return c;
},{});
First of all, you need to split the string into lines
Then split every line into two parts, the first part will be a key and the second one will be the value of the same key.
let input = `3S1,https://lekcjaplus.vulcan.net.pl
TA1,https://uonetplus-komunikacja.umt.tarnow.pl
OP1,https://uonetplus-komunikacja.eszkola.opolskie.pl
RZ1,https://uonetplus-komunikacja.resman.pl
GD1,https://uonetplus-komunikacja.edu.gdansk.pl
P03,https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl
P01,http://efeb-komunikacja.pro-hudson.win.vulcan.pl
P02,http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl
P90,http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl`;
output = input.split(/\n/g);
output = output.reduce((acc, item) => {
item = item.split(",");
acc[item[0]] = item[1];
return acc;
}, {})
console.log(output);

Categories