I have a string of values that I need to transpose to an object. I’ve encountered a weird issue where when the JS code is executed in the browser, it automatically converts certain characters into encoded HTML entities.
The code is as follows:
function split_to_objects(string) {
var output = {}
string.split('').reduce(function(object, value, index) {
output[index] = value;
}, {})
return output
}
var input_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu!"#$%&'(),-./0123456789:;<=>?@[\]^_`';
var output_obj = split_to_objects(input_string);
console.log(output_obj);
This produces the output of:
{
0: "A",
1: "B",
10: "K",
11: "L",
12: "M",
13: "N",
14: "O",
15: "P",
16: "Q",
17: "R",
18: "S",
19: "T",
2: "C",
20: "U",
21: "V",
22: "W",
23: "X",
24: "Y",
25: "Z",
26: "a",
27: "b",
28: "c",
29: "d",
3: "D",
30: "e",
31: "f",
32: "g",
33: "h",
34: "i",
35: "j",
36: "k",
37: "l",
38: "m",
39: "n",
4: "E",
40: "o",
41: "p",
42: "q",
43: "r",
44: "s",
45: "t",
46: "u",
47: "!",
48: """,
49: "#",
5: "F",
50: "$",
51: "%",
52: "&",
53: "'",
54: "(",
55: ")",
56: ",",
57: "-",
58: ".",
59: "/",
6: "G",
60: "0",
61: "1",
62: "2",
63: "3",
64: "4",
65: "5",
66: "6",
67: "7",
68: "8",
69: "9",
7: "H",
70: ":",
71: ";",
72: "<",
73: "=",
74: ">",
75: "?",
76: "@",
77: "[",
78: "",
79: "]",
8: "I",
80: "^",
81: "_",
82: "`",
9: "J"
}
As you can see some characters such as &
became &
or <
became <
. So my question is, how can we modify the function to produce the output object values that is identical to the original character like in the string?