The string I have is as like as follows
let a = "0j (0.001953125+0j) (-0.001953125+0.001953125j) (0.998046875+0j) (-0.001953125+0j) (0.001953125+0j) (0+0.0914587j)"
Info about the string:
1/ Each substring is complex number in the format of a+bj
2/ Possible format of the complex number could be a+bj,a,bj. Eg: 1+2j, 1,2j
3/ There is a space( ) between each substring
4/ I have seen that 0+bj(eg: 0+5j) or a+0j(eg: 5+0j) format is not possible/ created by the backend so this type of format/ presentation is not considered for my case.
5/ If the substring contains only real/imaginary part then parenthesis () will not be used. Eg: (5),(5j) is not possible. They will be 5,5j
I need to create a JSON or JavaScript object from that string which will be used to plot data. The data is coming from the Flask backend and it is different for each request. One approach I have found to make this JSON object is from an array which should look like
let my_array = [[0,0], [0.001953125,0], [-0.001953125,0.001953125], [0.998046875,0],[-0.001953125,0],[-0.001953125,0],[0,0.914587]]
But I am totally lost in making of this array. Initially, I have removed all the j from the string by a.replaceAll("j","") but then I have not found a way to make my desired array structure. If I get the array, I can make the JSON object with the following approach:
my_array = [[0,0], [0.001953125,0], [-0.001953125,0.001953125], [0.998046875,0],[-0.001953125,0],[-0.001953125,0],[0,0.914587]]
temp_key = ["i", "q"]
my_json = {
}
for(let a = 0; a < my_array.length; a++){
temp_json = {};
for(let b = 0; b < my_array[a].length; b++){
temp_json[temp_key[b]] = my_array[a][b];
}
my_json[String(a)] = temp_json;
}
console.log("my_json: ",my_json)
Suggestions regarding making this array will be appreciated.
jis for and what about+? Initially I thought j =0as a separate number, but then it's ignored in 3nd group?0.0.914587jlooks like typo? Did you mean0.0914587j?5j,5,-5-1j,-5j+2or+5-1jpossible?jis for imaginary part. The data is coming from Python Backend as string.+is also presentation type a+bj