Search code examples
javascriptarraysvb.nethashtable

convert from hastable result into a string using Java Script


I have a result of type of:

EMailLabel: "Mailing address"
LogLabel: "User login"
LoginButton: "Enter the program"

And in order to manipulate this result by splinting it into pairs, I need to convert it into string using the following:

 function parse(str, separator) {
       var parsed = {};
       var pairs = 
       str.toString().split(separator);
       for (var i = 0, len = pairs.length, keyVal; i < len; ++i) {
       keyVal = pairs[i].split("=");
       if (keyVal[0]) {
       parsed[keyVal[0]] = keyVal[1];
    }
}
return parsed;

} But in the instruction:

str.toString().split(separator);

returns me the value of:

{[object Object]: undefined}

And of nothing is turned into string.
If I use the same instruction like that:

str.split(separator);

Threw me the error of:

Uncaught TypeError: str.split is not a function

And from what I've searched on the web I saw that I have to convert the str which is a Hashtable result into string.
I did that but unfortunately without any success
Is someone to help me on this issue?


Solution

  • Looks like you need something like this:

    function parse(map, separator) {
       return Object.keys(map).reduce((data, key) => {
         data.push(`${key}${separator} "${map[key]}"`);
         return data;
       }, []).join('\n');
    }
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals