Recently, I received a string from the backend that looks like this:
var string = "{name: Hello, age: 20}, {name: Nadia, age: 30}"
. To transform it into an object, I attempted to push it into an empty array using the following code:
var array = [];
var array1 = array.push(string);
However, the result after appending to the array was not what I expected:
Array [
"{name: Hello, age: 20},
{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}"
]
What I really wanted was this:
Array [
"{name: Hello, age: 20}",
"{name: Nadia, age: 30}",
"{name: Nadia, age: 30}",
"{name: Nadia, age: 30}",
"{name: Nadia, age: 30}"
]
I tried using split(",")
, but each object got separated individually. Can anyone help me achieve the desired outcome? I'm still learning and would appreciate any guidance.
UPDATE: I followed the solution provided, but it didn't quite give me the result I was aiming for. The method returned two objects, with the second one being a combination of objects. Here's how the output looked using the suggested approach:
Array [
"{name: Hello, age: 20}",
"{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}"
]
Unfortunately, I have requested my backend colleague to adjust the JSON return, but they are unable to make changes. For now, I must find a solution on the frontend side to handle this issue.