2

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.

10
  • 1
    What j is for and what about +? Initially I thought j = 0 as a separate number, but then it's ignored in 3nd group? Commented Jun 13, 2021 at 1:15
  • 1
    0.0.914587j looks like typo? Did you mean 0.0914587j? Commented Jun 13, 2021 at 1:19
  • 1
    I think the question is unclear. Are values like 5j, 5, -5-1j, -5j+2 or +5-1j possible? Commented Jun 13, 2021 at 1:23
  • @vanowm j is for imaginary part. The data is coming from Python Backend as string. + is also presentation type a+bj Commented Jun 13, 2021 at 7:25
  • @jabaa no it is not possible. And I guess question is not unclear in general. Commented Jun 13, 2021 at 7:28

1 Answer 1

5

You can split by whitespace, then use a regular expression to match digit characters in the substring (eg 0.001953125+0j to 0.001953125 and 0).

const str = "0j (0.001953125+0j) (-0.001953125+0.001953125j) (0.998046875+0j) (-0.001953125+0j) (0.001953125+0j) (0+0.914587j)";
const arr = str
  .split(' ')
  .map((substr) => {
    const [real, imag = 0] = substr.match(/-?\d+(?:\.\d+)?/g).map(Number);
    return [real, imag];
  });
console.log(arr);

-?\d+(?:\.\d+)? is:

  • -? - possibly match a leading -
  • \d+ - match one or more digits
  • (?:\.\d+)? - optionally match the following (the decimal part of a number):
    • \. - a literal .
    • \d+ - one or more digits
Sign up to request clarification or add additional context in comments.

3 Comments

Your approach is solved most of the cases. But what about it const str = "5j";? For this I expect { 0: { i: 0, q: 5 } } But I am getting { 0: { i: 5, q: 0 } } i,q format is not desirable. Any idea to solve this?
That first substring does not match the format of all of the rest in parentheses. Does it have any other possible format, or is \dj the only possible substring there?
possible format of the substring could be a+bj, a or bj. Already I have tested with your solution that a+bj, a working fine. But while bj comes then it cannot distinguish between real and imaginary parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.